2017-07-03 199 views
1

我嘗試刪除文件,該文件是在/etc/scripts/PHP - 不能刪除文件

首先,我創建了一個演示文件:

echo "test1234" > test.log 

-RW-R - R-- 1個根根7月5日13:15 3 test.log中

現在,我嘗試使用PHP來刪除它:

deleteFile(); 

function deleteFile() 
{ 
    $file = "/etc/scripts/test.log"; 

    if (is_file($file)) { 
     chmod($file, 0777); 
     if (unlink($file)) { 
      return "File '$file' deleted."; 
     } else { 
      return "File '$file' could not be deleted."; 
     } 
    } else { 
     return "$file is not a file!"; 
    } 
} 

但我得到File '/etc/scripts/test.log' could not be deleted.作爲迴應;

我也對該文件執行了chmod 777 test.log,結果相同。

+2

檢查你的用戶自己的操作與否?這個操作可能被允許到'root'上。 – urfusion

+0

你可能沒有正確定位文件,可能是一些路徑問題。做'var_dump(file_exists($ file));'首先,在'deleteFile'函數 – samayo

+0

裏面你是否以控制檯的root身份執行chmod 777?這樣做從PHP不會改變任何東西(除非你已經設法讓PHP以root身份運行) – rypskar

回答

3

不僅文件本身,而且目錄/etc/scripts/應該由執行腳本的用戶寫入。

+0

你是對的,我必須給第三組寫權限,現在它可以工作:)但是我會用'shell_exec('sudo rm PATH');'來代替,並且在/ etc/sudoers,它比公開寫作更安全。 – Black