2013-07-30 52 views
0

我使用HTML表單上傳文件到服務器,並在服務器中創建TXT文件來編寫錯誤日誌,但此過程在我的本地系統和其他服務器中運行良好,但在一臺服務器中運行得不錯這個操作必須工作。在PHP中將文件上傳到服務器錯誤

錯誤消息:

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); 
} 
+3

向我們展示您使用的代碼。對於linux服務器,您可以使用chmod更改權限。 – Aris

+0

這不是一個perrmision。問題是你確定該文件夾存在或路徑正確。有時你必須給完整的路徑像/ var/www/smart_1 /文件夾 –

+0

是的文件夾和路徑存在這就是爲什麼它在我的其他服務器以及我的本地工作良好.. – TomPHP

回答

0

我已經通過以下方法解決了這個問題:

找到你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 

然後更改File Permission..

更改模式0777到下面的文件夾:

taskfinished 
errorlog 

這就解決了問題!

希望這有助於一個人!

0
如果你運行古都測試用例

,你得到了什麼錯誤訊息?

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,你需要的文件夾​​的所有者或許可改變(CHangeOWNer用戶Apache和組阿帕奇)或chmod o+w(改變模式爲業主增添寫) 3或4我不知道