Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ']'

like image 349
Tom Morison Avatar asked Apr 24 '16 23:04

Tom Morison


2 Answers

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

like image 166
wesamly Avatar answered Oct 11 '22 02:10

wesamly


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.

like image 38
Todd Christensen Avatar answered Oct 11 '22 01:10

Todd Christensen