● 写文件 和读取文件的方式一样,先看看是不是能写: <?php $file = 'dirlist.php'; if (is_writable($file) == false) { die("我是鸡毛,我不能"); } ?> 能写了的话可以使用file_put_contents函数写入: <?php $file = 'dirlist.php'; if (is_writable($file) == false) { die('我是鸡毛,我不能'); } $data = '我是可鄙,我想要'; file_put_contents ($file, $data); ?> file_put_contents函数在php5中新引进的函数(不知道存在的话用function_exists函数先判断一下)低版本的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)){//写入字节流,serialize写入其他格式 $this->warns('无法写入缓存文件.');//trigger_error return false; } flock($fso,LOCK_UN);//释放锁定 fclose($fso); return true; } 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fkedwgwy/archive/2008/06/04/2511639.aspx
发表评论