Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 file upload

I'm trying to upload some photos and handle this with the build in Laravel functions. But I can't for the life of me figure out how to do this properly. I have been able to actually upload something, but I've run into a few problems. This is the code I have right now:

If looked at the documentation, and found this function: $file = Input::file('photo'); I've used this function, and what the content of $file becomes is an instance of Symfony\Component\HttpFoundation\File\UploadedFile, which, as the documentation tells us, "extends the PHP SplFileInfo class and provides a variety of methods for interacting with the file." http://laravel.com/docs/4.2/requests#files

Then I used this function Input::file('photo')->move($destinationPath); which should but the file in the desired folder on the server. And it did. But now comes the problem. Now all uploaded files have a filename like phpgoJnLc, and without an extension.

I've looked at the functions available from SplFileInfo and tried getExtension which give me an empty string and getFilename which also gives me something like phpgoJnLc.

Then I looked around on the internet and found a few part of code from Laravel 3, where they did something like this:

$filename = Str::random(20) .'.'. File::extension(Input::file('photo.name'));

But the result of this is give me only the result from Str::random(20) followed by a dot. Again, no file extension.

So, what am I doing wrong? How to upload a file with Laravel 4?

like image 664
Crinsane Avatar asked Apr 14 '13 18:04

Crinsane


People also ask

How do I use Filepond?

Create a directory and call it /filepond. Now go to the directory on the command line and type npm init . Accept all the defaults. Now install the Express server with npm install express .


1 Answers

Looking in that same class file I see a getClientOriginalName() function...

$file = Input::file('photo');
$file->move($destinationPath,$file->getClientOriginalName());

... which ais ssuming you want to keep the original name your client sets... which could be hazardous, do some safety checks on it would be my advice. Getting the extensionname only is done with ->getClientOriginalExtension(), so you could also only save that part & add a random string before that in the second argument of the move() function.

like image 77
Wrikken Avatar answered Oct 04 '22 06:10

Wrikken