php文件读写操作实例代码
本教程举了大量的关于如何利用php来操作文件,如有截取文件,读取文件内容,格式化输出文件内容,锁定文件函数,以写入方式打开文件,csv文件等操作,实例代码如下:
- */
- $fp=fopen('test.txt','r');
- $flag=ftruncate($fp,5);
- if($flag)
- {
- echo "文件截取成功!";
- }
- else
- {
- echo "文件截取失败!";
- }
- fclose($fp);
-
-
-
-
-
- $fp=fopen('test.txt','r');
- $data=fgets($fp,4096);
- fseek($fp,100);
- fclose($fp);
-
-
-
-
- $fp=fopen("test.txt","r");
- $contents=fscanf($fp,"%st%st%sn");
- print_r($contents);
- fclose($fp);
-
-
-
- foreach(glob("*.txt") as $filename)
- {
- echo "文件名:".$filename;
- echo "<br>";
- echo "文件大小:".filesize($filename)."n";
- echo "<p>";
- }
-
-
-
- function lockfile($fp)
- {
- flock($fp,lock_ex)or die("cannot flock file");
- }
- function unlockfile($fp)
- {
- flock($fp,lock_un)or die("cannot unlock file");
- }
- $fp=fopen("test.txt","w+");
- lockfile($fp);
- fwrite($fp,"hello,php教程");
- unlockfile($fp);
- fclose($fp);
-
-
-
- $file="test.txt";
- $fp=fopen($file,"w");
- $date="hello world!";
- fputs($fp,$date);
- fclose($fp);
-
-
-
- $list=array(
- 'a,b,c,d',
- '1,2,3'
- );
- $fp=fopen('test.csv','w');
- foreach($list as $line)
- {
- fputcsv($fp,split(',',$line));
- }
- $date=fread($fp,4096);
- echo $date;
- fclose($fp);
-
-
-
- $fp=fopen("test.txt","r");
- echo fpassthru($fp)
- fclose($fp);