简体   繁体   中英

File fails to upload in laravel

Here is my routes file

Route::resource('item', 'ItemController');

Route::get('welcome', function() {
    return view('welcome');
});
Route::auth();

Route::get('/home', 'HomeController@index');
Route::get('/item', 'ItemController@store');

//Route
Route::post('/item', ['as' => 'item.store', 'uses' => 'ItemController@store']);

Here is my store controller

public function store(Requests\CreateItem $request)
{
    Item::create($request->all());
    $file = $request->file('filename');
    if (Input::hasFile('filename')) {
        echo 'Uploaded';
        $file = Input::file('filename');
        $file->move('uploads', $file->getClientOriginalName());
    }
}

My upload form does not return any errors, but this snippet of code is not moving my uploads to the folder specified. What am I doing wrong? I am using Laravel 5.2.

edit: Here is my form

   {!! Form::open(['url' =>'route' => 'item.store', 'files' => true]) !!}
    {!! Form::label('name', "Name") !!}
    {!! Form::text('name', null, ['class' => 'form-control']) !!}

   {!! Form::label('filename', "File Name") !!}
    {!! Form::file('filename', null, ['class' => 'form-control']) !!}

    {!! Form::label('description', 'Description') !!}
    {!! Form::textarea('description', null, ['class' => 'form-control']) !!}
    {!! Form::submit('Add Item', ['class' => 'btn btn-primary form-control']) !!}

EDIT 2: Now I'm getting the following error upon accessing the form

FatalErrorException in 41b177cdb949fd0263185e364012b35dff81db06.php line 7: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ']'

EDIT 2 Now I get this error everytime I try to access the add item form:

FatalErrorException in 41b177cdb949fd0263185e364012b35dff81db06.php line 7: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ']'

Another reason for this is PHP max upload size. Increase value and try again.

Does the <form> have enctype="multipart/form-data" on it? It needs this to send the files properly. It's practically an error whenever a <form> has <input type="file"> elements in it, but doesn't have that attribute.

This is a pretty common mistake, gets people all the time.

You forgot a coma and a key

{!! Form::open(['url' =>'route' **, 'key'** => 'item.store', 'files' => true]) !!}

and you should use url or route, but not both.

{!! Form::open(['route' => 'item.store', 'files' => true]) !!}

If everything seems fine, then you need to check your file. Test your upload with a different file with a lower size.

For me, it was solved when I resized the image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM