【问题标题】:move_uploaded_file permission denied on 777 folder777 文件夹上的 move_uploaded_file 权限被拒绝
【发布时间】:2014-06-20 16:38:14
【问题描述】:

这是我尝试通过 ajax 上传时收到的错误:

move_uploaded_file(/public/assets/uploads/photodune-5351272-asian-traditional-bell-l.jpg): failed to open stream: Permission denied

我的上传文件夹已经使用 chmod 设置为 777。这是我的文件夹的状态:

drwxrwxrwx 2 ec2-user ec2-user 4096 Jun 20 10:46 uploads

这是我在 PHP 端使用的代码:

if (Input::hasFile('file')) {

    $storeFolder = '/assets/uploads/';

    if (!empty($_FILES)) {
        $tempFile = $_FILES['file']['tmp_name'];
        $targetPath = public_path() . $storeFolder;
        $targetFile =  $targetPath. $_FILES['file']['name'];
        move_uploaded_file($tempFile, $targetFile);

        echo $targetPath . " / " . $targetFile;
    }

} else {
    echo "There has been an error in creating the file";
}

为什么我在尝试使用 move_uploaded_file 时仍然会被拒绝?

为了测试,您可以从这里查看文件上传和错误:http://www.relatient.net/relatientv2/public/portal/upload

【问题讨论】:

  • move_uploaded_folder?你是什​​么意思? move_uploaded_folder 不是函数,还是基于 Laravel 的函数?
  • 不要使用此代码。您直接使用['name'],恶意用户可以利用它在您的服务器上任意位置乱写他们的文件。而且您似乎根本没有验证上传,只是假设它成功,或者首先执行了上传。
  • 我正在使用这个语句来检查文件是否被上传:if (Input::hasFile('file'))。当我得到要实际上传的文件时,我将添加验证和干净的代码。而 move_uploaded_folder 应该是 move_uploaded_file。大脑无处不在。
  • 我记得昨天看到一个类似的问题,使用 public_path() 是问题所在,给出答案的人建议使用 asset 等。只是不记得所有细节。
  • Fred,这不是问题,我实际上已经给了它完整的路径,而没有使用任何函数,它仍然给了我与 public_path() 相同的响应。资产,它给你一个 html URL,而不是服务器上的本地路径。

标签: php laravel upload


【解决方案1】:

由于某种原因,资产文件夹也需要设置为 775,而 laravel 本身必须这样做:./composer.phar dumpautoload。

完成此操作后,一切正常上传。

【讨论】:

    猜你喜欢
    • 2012-05-03
    • 2012-07-27
    • 2012-06-08
    • 2012-05-09
    • 2018-08-21
    • 1970-01-01
    • 2019-07-12
    • 2014-01-27
    相关资源
    最近更新 更多