標籤:style class blog c code java
----- 024-file.php -----
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="content-type" content="text/html; charset=utf-8"> 5 <title>檔案處理</title> 6 </head> 7 <body> 8 <h2>檔案處理</h2> 9 <pre>10 <?php11 $f_zzz = iconv(‘utf-8‘, ‘gbk‘, ‘D:\a\res\txt\複姓UTF-8.txt‘);12 $fp_zzz = fopen($f_zzz, ‘r‘); //開啟檔案,返迴文件控制代碼13 $arr_zzz = file($f_zzz); // 檔案 => 數組14 $str_zzz = file_get_contents($f_zzz); // 檔案 => 字串15 $arr_temp = explode(‘ ‘, preg_replace(‘#\s+#‘, ‘ ‘, $str_zzz));16 var_dump($arr_temp);17 echo fread($fp_zzz, 100), "\n"; // 讀取檔案的100個位元組18 echo fgetc($fp_zzz), "\n"; //讀取一個字元19 echo fgets($fp_zzz), "\n"; //讀取一行20 echo fgetss($fp_zzz), "\n"; //讀取一行,過濾HTML PHP標記21 var_dump(fgetcsv($fp_zzz)); echo "\n"; //讀取一行,過濾HTML PHP標記22 echo fpassthru($fp_zzz);echo "\n"; // 讀取剩餘檔案23 echo readfile($f_zzz); //讀取全部檔案24 fclose($fp_zzz);25 ?>26 </pre>27 </body>28 </html>
----- 025-file_write.php -----
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="content-type" content="text/html; charset=utf-8"> 5 <title>寫檔案</title> 6 </head> 7 <body> 8 <h2>寫檔案</h2> 9 <pre>10 <?php11 $f_zzz = iconv(‘utf-8‘, ‘gbk‘, ‘D:\a\res\txt\php寫檔案測試.txt‘);12 $fp_zzz = fopen($f_zzz, ‘w‘);13 file_put_contents($f_zzz, "欲窮千裡目\n"); //寫入一個字串14 fwrite($fp_zzz, "白日依山盡\n");15 fwrite($fp_zzz, "黃河入海流\n");16 fputs($fp_zzz, "更上一層樓\n");17 echo "當前檔案指標位置:", ftell($fp_zzz), "\n";18 fseek($fp_zzz, 32); //移動檔案指標到第三行開頭19 fwrite($fp_zzz, "欲窮千");20 fseek($fp_zzz, -9, SEEK_CUR); //指標前移一行21 fwrite($fp_zzz, "長太息以掩涕兮\n");22 rewind($fp_zzz);23 fwrite($fp_zzz, "窗含西嶺千秋雪");24 25 26 27 28 29 fclose($fp_zzz);30 echo "檔案內容:\n".file_get_contents($f_zzz);31 ?>32 </pre>33 </body>34 </html>