Laravel file upload

末鹿安然 提交于 2019-12-13 04:42:46

问题


Getting the following error

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

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.


回答1:


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

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



回答2:


It can also be done like this:

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

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

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


来源:https://stackoverflow.com/questions/20805175/laravel-file-upload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!