Laravel 4 文件上传
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16002860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 file upload
提问by Crinsane
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:
我正在尝试上传一些照片并使用 Laravel 函数中的构建来处理此问题。但我一生都无法弄清楚如何正确地做到这一点。我已经能够实际上传一些东西,但我遇到了一些问题。这是我现在拥有的代码:
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
如果看文档,发现这个函数:$file = Input::file('photo');
我用过这个函数,内容$file
变成了 的一个实例Symfony\Component\HttpFoundation\File\UploadedFile
,正如文档告诉我们的,“扩展了 PHP SplFileInfo 类并提供了多种方法与文件交互。” 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.
然后我使用了这个函数Input::file('photo')->move($destinationPath);
,它应该是服务器上所需文件夹中的文件。它做到了。但现在问题来了。现在所有上传的文件都有一个类似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'));
$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.
但这样做的结果是只给我Str::random(20)
后跟一个点的结果。同样,没有文件扩展名。
So, what am I doing wrong? How to upload a file with Laravel 4?
那么,我做错了什么?如何使用 Laravel 4 上传文件?
回答by Wrikken
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 wantto 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.
...假设您想保留客户设置的原始名称...这可能很危险,我的建议是对其进行一些安全检查。只获取扩展名是用 完成的->getClientOriginalExtension()
,所以你也可以只保存那部分并在move()
函数的第二个参数之前添加一个随机字符串。
回答by Pathros
This worked for me, especially when you want to change the name of the uploaded image:
这对我有用,尤其是当您想更改上传图像的名称时:
$filename = 'New_Name'.'.'.Input::file('photo')->getClientOriginalExtension();
回答by Moe
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()'