move_uploaded_file()不起作用,除非destination是主目录(Ubuntu)

时间:2018-01-08 08:22:54

标签: php ubuntu

我想将上传的图像保存到目录中的文件夹中。我测试了这些目的地: $destination_path = __DIR__."/../img/";$destination_path = "../img/";以及$destination_path = "/../img/";

我也尝试了以下内容并且无效

chdir('../img/');
$destination_path = getcwd().DIRECTORY_SEPARATOR;

只有当文件放在网站主目录中时,才对我有用!!

 $destination_path = getcwd().DIRECTORY_SEPARATOR;

我的代码: -

$destination_path = "/../img/";
$name = rand (100,1000);
$result = 0;
$filename=$_FILES['myfile']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION) ;

   // setting up the directory of the file uploaded
$target_path = $destination_path . $name .".". $ext;

   //making sure the file has been uploaded in the specified directory
if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) 
 {
 $result = 1;
 }

1 个答案:

答案 0 :(得分:0)

我正在运行Ubuntu。问题是许可问题。我跑了这个来解决它:

 sudo chmod 777 /var/www/work_pathname/the_upload_pathname