2013-07-20 21 views
2

覆蓋文件我試圖重寫我在PHP 創建時我使用file_put_contents我收到的書面不能用PHP

與功能的「X」字節0低於使用文件的fwrite我得到的錯誤文件不可寫入。

的權限爲777(包括目錄)

是沒有問題的創建文件,問題是當我嘗試對其進行修改。

任何人都可以認爲是什麼問題?

function update_body($body) 
{ 
    $URL = $_REQUEST["URL"]; 
    $filename = $URL; 
    $somecontent = $body; 

    if (is_writable($filename)) 
    { 
     if (!$handle = fopen($filename, 'w')) 
     { 
      echo "Cannot open file ($filename)"; 
      exit; 
     } 

     if (fwrite($handle, $somecontent) === FALSE) 
     { 
      echo "Cannot write to file ($filename)"; 
      exit; 
     } 

     echo "Success, wrote ($somecontent) to file ($filename)"; 

     fclose($handle); 

    } 
    else 
    { 
     echo "The file $filename is not writable"; 
    } 
} 

其中i創建文件

function save_body($body) 
{ 
    $datearr = getdate(); //get date array, use date for folder, time for filename 
    $current_date = $datearr['year'].$datearr['mon'].$datearr['mday']; 
    $current_time = $datearr['hours'].$datearr['minutes'].$datearr['seconds']; 

    // create directory according the date 
    if (!file_exists($_SERVER['DOCUMENT_ROOT'] .'/articles/'.$current_date)) 
     mkdir($_SERVER['DOCUMENT_ROOT'] .'/articles/'.$current_date, 0777, true); 

    // write the file 
    $fp = fopen($_SERVER['DOCUMENT_ROOT'].'/articles/'.$current_date.'/'.$current_time.'.txt',"w"); 
    fwrite($fp,$body); 
    fclose($fp); 
    return $current_date.'/'.$current_time; 
} 
+0

你如何創建文件? – Sebas

+0

作爲第一個猜測,我會建議你可能看到的文件不是PHP試圖寫入的文件。你能否在你的答案中包括錯誤信息的輸出? –

+0

「當我使用file_put_contents」時 - 在代碼中看不到file_put_contents的調用。 – user4035

回答

0

歡迎堆棧溢出的程序。

正如@Sebas說,只要試試你的文件夾是

$_SERVER['DOCUMENT_ROOT'] 

一個愉快的一天有轉而使用相對路徑(../../../articles)或!