----- 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 <?php 11 $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 <?php 11 $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>