move_uploaded_file()无法打开流和move_uploaded_file():无法移动

问题描述 投票:0回答:2

我正在做一个文件上传页面,它会在添加文件之前重命名该文件,并且我收到了这些错误。我在网站的文件夹中尝试过chmod -R 777 ./,但它仍然无法正常工作。

 $dir = Users::currentUser()->id;//each user have his folder
if(move_uploaded_file($_FILES["file"]["tmp_name"], PROOT . 'files' . DS . $dir . DS))
    {
      echo "The file has been uploaded as ".$ran2.$ext;
        //Router::redirect('upload');
    }
    else
    {
      echo "Sorry, there was a problem uploading your file.";
    }

PROOT被我定义为网站根文件夹define('PROOT','/framework/');,而DS是分隔符(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? define('DS', '/') : define('DS', DIRECTORY_SEPARATOR);

这就是错误的样子

Warning: move_uploaded_file(/framework/files/4/): failed to open stream: No such file or directory in /opt/lampp/htdocs/framework/app/controllers/UploadController.php on line 46


Warning: move_uploaded_file(): Unable to move '/opt/lampp/temp/phpHz70FG' to '/framework/files/4/' in /opt/lampp/htdocs/framework/app/controllers/UploadController.php on line 46

我也从if声明中得到错误

Sorry, there was a problem uploading your file.

here是我的文件结构的图片

更新

问题是ubuntu权限的原因,在Windows上它完美无缺

php apache chmod
2个回答
0
投票

试试吧

define('PROOT','framework/');
(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? define('DS', '/') : define('DS', DIRECTORY_SEPARATOR)

$dir = Users::currentUser()->id;//each user have his folder
$path = PROOT . 'files' . DS . $dir . DS;
if(move_uploaded_file($_FILES["file"]["tmp_name"],$path)
    {
      echo "The file has been uploaded as ".$ran2.$ext;
        //Router::redirect('upload');
    }
    else
    {
      echo "Sorry, there was a problem uploading your file.";
    }

0
投票

你必须使用绝对路径

 define('PROOT','/opt/lampp/htdocs/framework/');
© www.soinside.com 2019 - 2024. All rights reserved.