Download Laravel 4 File

I am trying to upload some photos and handle this using assembly in Laravel functions. But I canโ€™t understand for my whole life how to do this properly. I really could download something, but I was having problems. This is the code that I have right now:

If you look at the documentation and find this function: $file = Input::file('photo'); I used this function and the contents of $file becomes an instance of Symfony\Component\HttpFoundation\File\UploadedFile , which, as the documentation tells us, extends the PHP class SplFileInfo and provides many 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 is in the right folder on the server. And that happened. But now there is a problem. Now all downloaded files have a file name, for example phpgoJnLc , and without the extension.

I 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 the internet and found several pieces 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 gives me the result only from Str::random(20) , followed by a dot. Again, the file extension.

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

+7
source share
3 answers

In the same class file, I see the getClientOriginalName() function ...

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

... that you want to keep the original name that your client sets ... which can be dangerous, doing some security checks on this will be my advice. Getting the extension name is done only with ->getClientOriginalExtension() , so you can also save this part and add a random string before that in the second argument to the move() function.

+17
source

This worked for me, especially if you want to change the name of the uploaded image:

 $filename = 'New_Name'.'.'.Input::file('photo')->getClientOriginalExtension(); 
+1
source

You can also generate a file name with the original file extension as follows:

 $filename = Str::random(20) . '.' . Input::file('image')->guessExtension(); 

if you try to use the following in Laravel 4:

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

you will get this error:

 'Call to undefined method Illuminate\Filesystem\Filesystem::guessExtension()' 
0
source

All Articles