Laravel 文件上传

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20805175/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:49:00  来源:igfitidea点击:

Laravel file upload

phpfileuploadlaravel

提问by Raggaer

Getting the following error

得到以下错误

Unable to create the "http://website.com/public/uploads/" directory

无法创建“ http://website.com/public/uploads/”目录

My code:

我的代码:

$file = Input::file('upload');
$file_name = $file->getClientOriginalName();
$file_size = round($file->getSize() / 1024);
$file_ex = $file->getClientOriginalExtension();
$file_mime = $file->getMimeType();

if (!in_array($file_ex, array('jpg', 'gif', 'png'))) return Redirect::to('/')->withErrors('Invalid image extension we just allow JPG, GIF, PNG');

 $newname = $file_name;
 $file->move(URL::to('/').'/uploads/', $newname);

The uploads folder exists.

上传文件夹存在。

回答by Antonio Carlos Ribeiro

You are trying to move a file to a URL, you have to move to a folder:

您正在尝试将文件移动到 URL,您必须移动到文件夹:

$file->move(base_path().'/public/uploads/', $newname);

回答by Nasim

It can also be done like this:

也可以这样做:

$file->move('uploads', $newname);

For a unique file name, you can add time()function like this:

对于唯一的文件名,您可以添加如下time()函数:

$file_name = time().$file->getClientOriginalName();
$file->move('uploads', $newname);