We discussed before form creation and validation but file upload input is a bit different. Let’s create our first file upload with laravel.
Before retrieving uploaded file data, We need to setup a simple form and two routes. The first route is for form view and the other route for form submission.
<!--app/views/form.blade.php-->
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
Laravel
</title>
</head>
<body>
{{ Form::open(array('url'=>'form-submit','files'=>true)) }}
{{ Form::label('file','File',array('id'=>'','class'=>'')) }}
{{ Form::file('file','',array('id'=>'','class'=>'')) }}
<br/>
<!-- submit buttons -->
{{ Form::submit('Save') }}
<!-- reset buttons -->
{{ Form::reset('Reset') }}
{{ Form::close() }}
</body>
</html>
//app/routes.php
Route::get('form', function(){
return View::make('form');
});
Route::any('form-submit', function(){
var_dump(Input::file('file'));
});
Basically, Uploaded files aren’t stored in the $_GET
or $_POST
array but they stored in the $_FILES
array. Fortunately laravel provide a great API to access$_FILES
array. To retrieve data of uploaded files, You can use Input::file()
method. Let’s dump the value of Input::file()
and explore the resulted object.
object(Symfony\Component\HttpFoundation\File\UploadedFile)[9]
private 'test' => boolean false
private 'originalName' => string 'test.jpg' (length=22)
private 'mimeType' => string 'image/jpeg' (length=10)
private 'size' => int 667220
private 'error' => int 0
Well. You can interact with these data by a bunch of methods. ThegetFilename()
method used to get temporary file name given to our file on temporary location. Let’s try this method.
Route::any('form-submit', function(){
return Input::file('file')->getFilename();
});
The getClientOriginalName()
method used to get the actual name of the file when it was uploaded.
Route::any('form-submit', function(){
return Input::file('file')->getClientOriginalName();
});
The getClientSize()
method used to get the size of the uploaded file in bytes.
Route::any('form-submit', function(){
return Input::file('file')->getClientSize();
});
The getClientMimeType()
method used to get the mime type of uploaded file.
Route::any('form-submit', function(){
return Input::file('file')->getClientMimeType();
});
The guessClientExtension()
method used to get extension of uploaded file.
Route::any('form-submit', function(){
return Input::file('file')->guessClientExtension();
});
The getRealPath()
method used to get current location of uploaded file.
Route::any('form-submit', function(){
return Input::file('file')->getRealPath();
});
The move()
method used used to move uploaded file to another location. The first parameter is the new destination while the second parameter is the new name. Let’s see how method work.
Route::any('form-submit', function(){
return Input::file('file')->move(__DIR__.'/storage/',Input::file('file')->getClientOriginalName());
});
hey,
you can also utilize the storage_path to get the path to the storage directory so your final code can be like
Input::file(‘file’)->move(storage_path(),Input::file(‘file’)->getClientOriginalName());
so that if you change the path of your storage you dont need to change your code later .
Thanks zaher. we should use storage_path regardless of specifying the real path.
Thz for the tutorial.
I am working on a file upload form and I am running into an issue where if a user uploads a file with the same name, it overwrites the existing file in storage. I am using the getClientOriginalName() function, but was wondering how to make sure that if the file already exists, to append a character to the filename so that it does not overwrite the existing file. Is this possible?
I was able to solve my issue. I added a “str_random(8)” parameter to my file path so that the file names could potentially be duplicates but the file will stay safe.
Yes, it is possible you can use
\Illuminate\Filesystem\Filesystem
class to list all files in the directory usingallFiles(string $directory)
method and check if file name already exist but I think you should create two storages, the first where client upload files to and then application change file name to a long secure name saved in database and file transferred to the second storage to be safe.thx for sharing this tutorial
Hi!! I made an upload function using Laravel and Windows 7! But I have a problem, i can’t upload audio or video files there’s only images that are uploaded. Does anyone know why?
Thanks!
You should check
upload_max_filesize
inphp.ini
file.Thanks for your answer! I did change the filesize in the php ini but it still isn’t working! I get this error while uploading a video or an audio file : “Call to a member function getClientOriginalName() on a non-object”.
I don’t get this error when i upload images!
You should share the code through stackoverflowhttp://stackoverflow.com/questions/tagged/laravel orhttp://laravel.io/ so I or laravel community can help.
Thanks for article.
less solution
public_path().’/files/’;
I appreciate the work as it would be always very helpful for beginners. Thanks
Thank you so much, this is all i need to get all the details of the files i’ve selected , cheers!
Thks a lot for the tutorial it is so useful!!! i need to store de file as blob, but this is a good begining