将文件上载到服务器时出现PHP错误

将文件上载到服务器时出现PHP错误,php,linux,file-upload,upload,hosting,Php,Linux,File Upload,Upload,Hosting,我正在使用HTML表单将文件上载到服务器,并在服务器中创建一个TXT文件以写入错误日志,但此过程在我的本地系统和其他服务器中运行良好,但在必须执行此操作的服务器中没有运行 错误消息: Warning: move_uploaded_file(taskfinished/512557562_348011_RAND_488.png): failed to open stream: Permission denied in /var/www/smart_1/JSON/taskimageupload.php

我正在使用HTML表单将文件上载到服务器,并在服务器中创建一个TXT文件以写入错误日志,但此过程在我的本地系统和其他服务器中运行良好,但在必须执行此操作的服务器中没有运行

错误消息:

Warning: move_uploaded_file(taskfinished/512557562_348011_RAND_488.png): failed to open stream: Permission denied in /var/www/smart_1/JSON/taskimageupload.php on line 70 

Warning: move_uploaded_file(): Unable to move '/tmp/phpz3HWEJ' to 'taskfinished/512557562_348011_RAND_488.png' in /var/www/smart_1/JSON/taskimageupload.php on line 70 

Warning: fopen(errorlog/07.30.13.txt): failed to open stream: Permission denied in /var/www/smart_1/JSON/errorlog.php on line 30 

Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/smart_1/JSON/errorlog.php on line 35 

Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/smart_1/JSON/errorlog.php on line 37 
我关闭了安全模式,但问题仍然是一样的

如何更改服务器中的用户权限

我的PHP代码:

$finenamedynamic = $CompanyId."_".$ShibNo."_RAND_".rand(100,600);
    //$finenamedynamic = date("Y-m-d-H_i_s")."_RAND_".rand(100,600);
    //New file name with EXTENSION
    $newfilename=$finenamedynamic.".".$extension;
    // Upload file
    $uploadedimage = $targetfilename . $newfilename;
    //Move file to the new file path
    if(!move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $uploadedimage)){
        //Error Log
        $message = "Error uploading file - check destination is writeable ". $uploadedimage;
        ErrorsAreAwesome('70','taskimageupload.php','Create Directory',$message);
    }

function ErrorsAreAwesome($line=null,$filename=null,$function=null,$message=null){
    //Create Error file for the day
    $myFile = "errorlog/".date("m.d.y").".txt";
    $error_time = date("Y-m-d H:i:s");
    //Construct Error
    $file_write = "[".$error_time."]-->[Line:".$line."]-->[File:".$filename."]-->[Fun:".$function."]-->[Msg:".$message."]";

    //if file exist appen in the file else create new file and write
    if (file_exists($myFile)) {
    $fh = fopen($myFile, 'a');
    } else {
      $fh = fopen($myFile, 'w');
    } 
    //write in file \r\n support for this OS or \n alone work
    fwrite($fh, "\r\n".$file_write."\n");
    //close file after writing
    fclose($fh);
}

如果您运行一千个测试用例,您会得到什么错误消息

if(!is_dir('taskfinished'))
{
   die("1: dir taskfinished dosn't exists\n");
}

if(!is_writable('taskfinished'))
{
   die("2: dir taskfinished isn't writeable\n");
}

$file_resource = fopen('taskfinished/test.txt', 'w');
if(!$file_resource)
{
   die("3: failed to open file\n");
}

if(!fwrite($file_resource, 'test'))
{
   fclose($file_resource);
   die("4: Failed to write to file\n");
}

fclose($file_resource);
die("All test passed\n");
对于1,您需要创建文件夹
mkdir taskfinished

对于2,您需要更改文件夹的所有者或权限
chown apache:apache taskfinished
(将所有者更改为用户apache和组apache)或
chmod o+w
(更改所有者添加写入的模式)
对于3或4年,我没有任何线索

我通过以下方式解决了这个问题:

在服务器中找到您的
php.ini
文件,在我的例子中,它位于
usr/Apache/bin

并查找以下代码:

; Safe Mode
; http://php.net/safe-mode
safe_mode = On
将上面的代码更改为下面的代码

; Safe Mode
; http://php.net/safe-mode
safe_mode = Off
然后更改
文件权限..

将模式0777更改为以下文件夹:

taskfinished
errorlog
问题就这样解决了


希望这能帮助一些人

向我们展示您使用的代码。对于linux服务器,您可以使用chmod来更改权限。这不是一个命令。问题:是否确定文件夹存在或路径正确。因为有时候你必须给出完整的路径,比如/var/www/smart_1/folder是的,文件夹和路径存在,这就是为什么它在我的其他服务器和本地服务器上运行良好..我现在添加了代码@阿里斯