1

我已经在 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');

4

1 回答 1

22

你的表格是什么样的?

在 Laravel 4 中,如果您要上传文件,则需要打开文件的表单

{{  Form::open(array('url' => 'my/path', 'files' => true)) }}

打开文件的表单后,getClientOriginalName() 方法将起作用。

还要确保您的输入是针对文件的

{{ Form::file('file') }}
于 2013-08-14T15:32:09.183 回答