[英]File upload in laravel 4
我已經在Laravel 4的控制器和路由文件中對此進行了編碼,並遇到了諸如“在非對象上調用成員函數move()”之類的錯誤,並且
"Call to a member function getClientOriginalName() on a non-object"
控制器:
class AuthorsController extends BaseController{
public $restful = true;
public function post_files()
{
$input = Input::all();
$rules = array(
'file' => 'image|mime:jpg,gif,png|max:3000',
);
$validation = Validator::make($input, $rules);
if ($validation->fails())
{
return Response::make($validation->errors->first(), 400);
}
$file = Input::file('file'); // your file upload input field in the form should be named 'file'
$destinationPath = 'public/uploads/'.str_random(8);
// $filename = $file->getClientOriginalName();
$filename = $file['name'];
//$extension =$file->getClientOriginalExtension(); //if you need extension of the file
$uploadSuccess = Input::file('file')->move($destinationPath, $filename);
if( $uploadSuccess ) {
return Response::json('success', 200); // or do a redirect with some message that file was uploaded
} else {
return Response::json('error', 400);
}
}
}
路線:
路線:: post('post_files','AuthorsController @ post_files');
您的表格是什么樣的?
在Laravel 4中,如果要上傳文件,則需要打開文件表單
{{ Form::open(array('url' => 'my/path', 'files' => true)) }}
打開文件表單后,方法getClientOriginalName()將起作用。
還要確保您輸入的是文件
{{ Form::file('file') }}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.