我正在尝试使用laravel4
来upload
和image
,我已经成功地从form
上传了file
,并将其存储在我选择的公共文件夹中的目录中。然而,我遇到的问题是查看图像。我正在Netbeans
开发,当我试图打开我新上传的图像时,我被告知图像已损坏。
这是我的表单:
<form action="{{ action('BookController@handleCreate') }}" method="post" role="form" enctype="multipart/form-data">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" />
</div>
<div class="form-group">
<label>Description</label>
<input type="textarea" class="form-control" name="desc" />
</div>
<!-- Img upload -->
<input type="file" name="img"/>
<input type="submit" value="Create" class="btn btn-primary" />
<a href="{{ action('BookController@index') }}" class="btn btn-link">Cancel</a>
</form>
这是我的controller
:
public function handleCreate(){
$book = new Book;
$book->book_name = Input::get('name');
$book->book_desc = Input::get('desc');
$destinationPath = public_path().'/uploads/covers/';//Set up destination path
$file = Input::file('img');//Get the file
$extension = $file->getClientOriginalExtension();//Get the extension
$filename = str_random(12).".".$extension;//Create a filename
$upload_success = $file->move($destinationPath, $filename);//Move the file to the destination
$pathToFile = '/uploads/covers/'.$filename;
echo $pathToFile;
//If successful.....
if($upload_success){
$book->book_cover = $filename;//store value in db
$book->save();//Save the book
return Redirect::action('BookController@index');
}else{//Else return to form with an error message....
return Response::json('error', 400);
}
}
这是我的图像的path
/uploads/covers/VFBDJPqEdI6P.jpg
我的计划是,我将在database
中存储此路径,并使用它在屏幕上显示图像。
我不知道为什么,但是当我上传文件时,它们被损坏了,如果有人能建议我解决这个问题,我将不胜感激。
对
我试过了,没有丢失任何内容
Storage::put($pathToFile,file_get_contents($request->file('img')->getRealPath()));