简体   繁体   English

Laravel 4文件上传

[英]Laravel 4 file upload

I'm trying to upload some photos and handle this with the build in Laravel functions. 我正在尝试上传一些照片并使用Laravel功能构建来处理这些照片。 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'); 如果查看文档,发现这个函数: $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." 我已经使用了这个函数, $file的内容变成了Symfony\\Component\\HttpFoundation\\File\\UploadedFile一个实例,正如文档告诉我们的那样,“扩展PHP SplFileInfo类并提供了各种方法与文件交互。“ http://laravel.com/docs/4.2/requests#files http://laravel.com/docs/4.2/requests#files

Then I used this function Input::file('photo')->move($destinationPath); 然后我使用了这个函数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. 现在所有上传的文件都有像phpgoJnLc这样的文件名,没有扩展名。

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 . 我查看了SplFileInfo可用的函数,并尝试了getExtension ,它给了我一个空字符串和getFilename ,它也给了我像phpgoJnLc

Then I looked around on the internet and found a few part of code from Laravel 3, where they did something like this: 然后我在互联网上环顾四周,从Laravel 3中找到了一些代码,他们做了类似这样的事情:

$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. 但结果是只给出了Str::random(20)后跟一个点的结果。 Again, no file extension. 同样,没有文件扩展名。

So, what am I doing wrong? 那么,我做错了什么? How to upload a file with Laravel 4? 如何使用Laravel 4上传文件?

Looking in that same class file I see a getClientOriginalName() function... 查看同一个类文件,我看到一个getClientOriginalName()函数...

$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. ... as ssuming你想要保留客户设置的原始名称......这可能是危险的,对它进行一些安全检查将是我的建议。 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. 仅使用->getClientOriginalExtension()来获取扩展名,因此您也可以只保存该部分并在move()函数的第二个参数之前添加一个随机字符串。

这对我有用,特别是当您想要更改上传图像的名称时:

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

You can also generate a name for the file with the original file extension like so: 您还可以使用原始文件扩展名为文件生成名称,如下所示:

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

if you try to use the following in Laravel 4: 如果您尝试在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()'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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