標籤:exit bst .net 例子 config dea bre new bsp
1.需求
瞭解php對檔案的一些操作
2.例子
寫了一個類,可以操作檔案,包含增,刪,查
<?phpclass myfile{ public function write_file($string,$path,$mode=‘w+b‘) { if(!$fp [email protected]fopen($path,$mode)) { echo "無法寫入檔案"; exit(); } if(flock($fp,LOCK_EX)) { for($written =0,$length = strlen($string);$written<$length;$written+=$result) { if(($result =fwrite($fp,substr($string,$written)))===false) { break; } } flock($fp, LOCK_UN); } else { echo "無法獲得檔案鎖"; exit(); } fclose($fp); return true; } public function read_file($path) { if(!$fp [email protected]fopen($path,‘r+b‘)) { echo "無法開啟檔案"; exit(); } if(flock($fp,LOCK_SH)) { if(!$content = fread($fp,filesize($path))) { echo "無法讀取檔案"; exit(); } } else { echo "無法獲得檔案鎖"; exit(); } fclose($fp); return $content; } public function delete_file($path) { if(!file_exists($path)) { echo "檔案不存在"; return false; } @unlink($path); return true; }}$config=array( ‘string‘=>‘abcdeaisdiiee‘, ‘path‘=>md5(microtime()).‘.php‘);$obj = new myfile();$obj->write_file(‘sufi‘,‘due‘.‘.php‘,‘a+b‘);
參考文檔:http://www.php.net/manual/zh/function.fwrite.php
php檔案類