2012-06-08 64 views
0

在php編程的年齡後,這個問題似乎對我來說真的很奇怪。php設置權限文件,使其可寫

我想製作一個文件dinamically寫入沒有生產/開發權限的問題....但它仍然給我麻煩。

有人可以給我解釋我錯了什麼?

// permissions -- the folder writable by www-data 
//drwxrwxrwx 2 www-data www-data 4096 mag 24 12:19 Details 

// then the file not writable by the owner 
-r----x--t 1 www-data www-data 0 giu 8 12:48 /home/giuseppe/homeProj//Ariprova/trunk/PhpCsv/phpcsv/ConfigClasses/Helpers/Virtuemart2/Details/324-PartsToAdd.json 

// then the code 

if (!file_exists($fileRequest)) { // it's found 
       throw new Exception ('File non trovato. Nome File completo: '.$fileRequest. ' con cartella '. getcwd()); 
       } 

if (!is_writable($fileRequest)) { 
       $results = @chmod($fileRequest, '0777'); //this gives true 

      } 

      $fileReq = fopen($fileRequest, 'w'); 
      fwrite($fileReq, 'a string'); // this writes nothing on file 

      fclose($fileReq); 
+0

您是否嘗試過使用'chmod'而不是'@ chmod'(這會抑制錯誤消息)? – Michael

+0

也嘗試chmod($ fileRequest,777); – Rishabh

+0

@Rishabh 777十進制不是0777八進制。 – lanzz

回答

6

更改chmod($fileRequest, '0777')chmod($fileRequest, 0777)。字符串'0777'將被轉換爲數字值,該值將是777十進制,這不是您所期望的;你真正想要的是0777八進制。

+1

我的天啊!我可能有多愚蠢? – giuseppe

1

這裏是搭配chmod手冊頁:http://php.net/chmod 你可以看到函數「嘗試更改指定文件的模式」

這是因爲運行腳本的用戶(例如apache)可能不允許覆蓋該目錄所有者設置的權限(至少這可能是其中一個原因)

更新:您可以嘗試轉到父目錄與所有者(或[R或者只是爲了確保),並將所有權更改爲您需要能夠執行此操作的用戶(chown apache:apache dirname或類似的東西)