2012-11-28 42 views
0

的文件有777次訪問時,文件中包含一些文本。 我得到了一個警告說:警告:fread():長度參數必須大於0在 ...PHP的fread不返回字符串

$ contents變量結束了不包含任何內容?有任何想法嗎? 該目錄必須正確,因爲如果我刪除該文件,我的is_readable返回false。

+0

什麼'的var_dump(文件大小($文件名));'說明了什麼? –

+0

int(0)......... – Jimmyt1988

+0

該文件的內容是:「email to:************************ * at:28/11/2012 03:05:48 pm「 – Jimmyt1988

回答

3

你是truncating當你最初用這一行打開它的文件。

$handle = fopen($filename, "w+"); 

所以規模將是零,在這一點上觸發該警告。

你或許應該做這樣的事情:

function log_email($message){ 
    $dir = dirname(__FILE__); 
    $filename = $dir . "/logs/email_log.txt";  
     echo $filename; 

    if(is_readable($filename)){   
     // This will append to the file without destroying its contents 
     $handle = fopen($filename, "a+"); 
     fwrite($handle, $message . "\n"); 
     fclose($handle); 
    }else{ 
     echo 'The file is not readable.'; 
    } 
} 
+0

不錯。謝謝。 a +完成了這項工作。 – Jimmyt1988