php -- 檔案讀寫

來源:互聯網
上載者:User

標籤: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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.