● 寫文件 和讀取文件的方式一樣,先看看是不是能寫: <?php $file = 'dirlist.php'; if (is_writable($file) == false) { die("我是雞毛,我不能"); } ?> 能寫了的話可以使用file_put_contents函數(shù)寫入: <?php $file = 'dirlist.php'; if (is_writable($file) == false) { die('我是雞毛,我不能'); } $data = '我是可鄙,我想要'; file_put_contents ($file, $data); ?> file_put_contents函數(shù)在php5中新引進(jìn)的函數(shù)(不知道存在的話用function_exists函數(shù)先判斷一下)低版本的php無法使用,可以使用如下方式: $f = fopen($file, 'w'); fwrite($f, $data); fclose($f); 替換之. 寫文件的時候有時候需要鎖定,然后寫: function cache_page($pageurl,$pagedata){ if(!$fso=fopen($pageurl,'w')){ $this->warns('無法打開緩存文件.');//trigger_error return false; } if(!flock($fso,LOCK_EX)){//LOCK_NB,排它型鎖定 $this->warns('無法鎖定緩存文件.');//trigger_error return false; } if(!fwrite($fso,$pagedata)){//寫入字節(jié)流,serialize寫入其他格式 $this->warns('無法寫入緩存文件.');//trigger_error return false; } flock($fso,LOCK_UN);//釋放鎖定 fclose($fso); return true; } 本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/fkedwgwy/archive/2008/06/04/2511639.aspx
發(fā)表評論