【问题标题】:Laravel file uploadLaravel 文件上传
【发布时间】:2014-01-15 07:26:40
【问题描述】:

得到以下错误

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

我的代码:

$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);

上传文件夹存在。

【问题讨论】:

  • 听起来目录不可写。尝试 chmod 777 或 755 并将您的网络服务器用户作为目录所有者。另外:在尝试移动文件时,您应该使用绝对文件路径而不是 URL。

标签: php file upload laravel


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    也可以这样:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 2013-07-22
      • 2013-04-06
      • 2015-03-21
      • 2017-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多