Laravel file upload

2019-09-09 03:36发布

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.

2条回答
\"骚年 ilove
2楼-- · 2019-09-09 03:44

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);
查看更多
在下西门庆
3楼-- · 2019-09-09 04:05

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

$file->move(base_path().'/public/uploads/', $newname);
查看更多
登录 后发表回答