PHP文件操作函数_PHP教程

WBOY
Freigeben: 2016-07-13 17:45:58
Original
743 Leute haben es durchsucht

 1.函数 file('目标文件')
把文件以数组的形式读出来,用循环方式遍历数组输
1.  
2. header('Content-Type:text/html; charset=utf-8'); 
3. $file=file("test.txt"); 
4. if($file) 
5. { 
6.     foreach ($file as $num=>$content) 
7.     { 
8.         echo "行数为 ".$num." 内容为 ".$content."
"; 
9.     } 
10. } 
11. else
12. { 
13.     echo "文件读取失败"; 
14. } 
15. ?>
输出结果为
 
2.函数fopen('要打开的文件','用何种方式') 打开一个文件,方式有R R+ W W+ AB等等
如果使用W,那么没有该文件会自动创建该文件
1.  
2. header('Content-Type:text/html; charset=utf-8'); 
3. $fopen=fopen('t2.php','w'); 
4. if($fopen) 
5. { 
6.     echo "文件打开成功"; 
7. } 
8. else
9. { 
10.     echo "文件打开失败"; 
11. } 
12. ?>
输出结果
文件打开成功 并自动创建该文件
3.函数fwrite('目标文件','写入的内容'),给目标文件写入内容
1.  
2. header('Content-Type:text/html; charset=utf-8'); 
3.  $c2="有一天我心血来潮带他去赶集"; 
4. $fp=fopen("t1.php","ab"); 
5. 
6. if (fwrite($fp,$c2)) 
7. { 
8.     echo "写入成功"; 
9. } 
10. else
11. { 
12.     echo "写入失败"; 
13. } 
14. if (fclose($fp)) 
15. { 
16.     echo "文档已经关闭"; 
17. } 
18. ?>
结果为,写入成功,文件t1.php 写入$c2内容
4.readfile('目标文件')读取目标文件或网站,与file不同的是直接读取文件的内容
1.  
2. 
3. $fp=readfile("http://www.baidu.com"); 
4. if (!$fp) 
5. { 
6.     echo "文件读取失败"; 
7. } 
8. ?>
输出结果
 
5.filesize('目标文件') 读出目标文件的大小
1.  
2. $filename=("t1.php"); 
3. $size=filesize($filename); 
4. echo $size."byts."; 
5. ?>

 
输出结果,186byts
6.feof('目标文件')判断目标文件指针是否到最后一行
1.  2. header('Content-Type:text/html; charset=utf-8'); 
3. 
4. $filename="test.txt"; 
5. if (file_exists($filename)) 
6. { 
7. $file = fopen($filename, "r"); 
8. 
9. //输出文本中所有的行,直到文件结束为止。 
10. while(! feof($file)) 
11.   { 
12.   echo fgets($file,4096). "
"; 
13.   } 
14. 
15. fclose($file); 
16. } 
17. else
18. { 
19.     $file = fopen($filename,"w"); 
20.     $fw=fwrite($file,$content); 
21.     if ($fw) 
22.     { 
23.         $file = fopen($filename,"r"); 
24.     while (!feof($file)) 
25.     { 
26.         echo fgets($file)."
"; 
27.          
28.     } 
29.     } 
30.     else
31.     { 
32.         echo "读取失败"; 
33.     } 
34.     fclose($file); 
35. } 
36. ?>
输出结果 test.txt中的内容
通过fget遍历数组,要点 打开文件时,方式必须是R才可以用fget遍历数组
7.unlink('目标文件') 删除目标文件
1.  
2. header('Content-Type:text/html; charset=utf-8'); 
3. $filename = "t1.php"; 
4. if(file_exists($filename)) 
5. { 
6.     unlink($filename); 
7.     echo $filename."文件删除成功!!"; 
8. } 
9. else
10. { 
11.     echo "找不到该文件"; 
12. } 
13. ?>
 
输出结果 t1.php 文件删除成功。
8.copy('目标文件','复制的位置') 复制的位置可以是当前目录,也可以指定精确位置
1.  
2. header('Content-Type:text/html; charset=utf-8'); 
3. $filename="test.txt"; 
4. if (copy($filename,"d:\test12.txt")) 
5. { 
6.     echo "复制文件成功"; 
7. } 
8. else
9. { 
10.     echo "复制文件失败"; 
11. } 
12. ?>
9
9.mkdir('文件夹名')创建一个文件夹
1.  
2. header('Content-Type:text/html; charset=utf-8'); 
3. 
4. function mk($dir) 
5. { 
6.      
7. if (file_exists($dir) && is_dir($dir)) 
8. { 
9.     echo "该文件夹名存在"; 
10. } 
11. else
12. { 
13.     if (mkdir($dir,0777)); 
14.     echo $dir."创建成功"; 
15. } 
16. } 
17. mk(date("Y-m-d")); 
18. ?>
输出结果,2011-09-20 创建成功,例子中使用声明一个函数的方式
10.rmdir('目标文件夹') 删除文件夹,注,无法删除非空文件夹,如果要删除非空文件夹,需要遍历文件夹里所有内容,然后先删除文件,再删除文件夹
例子
1.用realpath函数得到真实的地址,如果地址等于空,等于/,或者地址等于:\\ 那么证明是根目录,不能删除,返回假
2.如果不等于1中的内容,那么使用opendir函数打开目录句柄,返回一个目录流
用while (readdir())遍历目录,并且赋值给$file
3.如果$file不等于假的时候,如果等于.或者..的时候,continue
4.给$path赋值为 $dir(就是真实地址)连接接DIRECTORY_SEPARATOR(系统分隔符)连接$file. 这个地址为文件夹中文件的地址
5.当$path 为目录且rmdir($path)函数不为假时 就是当$path是目录但是不能删除时
unlink($path); 删除这个文件。关闭句柄。再删除文件夹。
1.  
2. header('Content-Type:text/html; charset=utf-8'); 
3. 
4. function mrdirs($dir) 
5. { 
6.      
7.     $dir = realpath($dir); 
8.     if($dir=='' || $dir=='/' || (strlen($dir)==3 && substr($dir,1)==':\\')) 
9.     { 
10.         return false; 
11.     } 
12.     else
13.     { 
14.         if(false != ($dh=opendir($dir))) 
15.         { 
16.             while(false != ($file=readdir($dh))) 
17.             { 
18.                 if($file=='.' || $file=='..') {continue;} 
19.                 echo $path=$dir .DIRECTORY_SEPARATOR . $file; 
20.                  if (is_dir($path)) 
21.                  { 
22.                      if(!rmdir($path)){return false;}} 
23.                      else
24.                      { 
25.                          unlink($path); 
26.                          echo $path."文件以删除"; 
27.                      } 
28.                  } 
29.                  closedir($dh); 
30.                  rmdir($dir); 
31.                  echo "删除文件夹成功"; 
32.                  return true; 
33.             } 
34.              
35.         else
36.         { 
37.             return false; 
38.         } 
39.                  
40.              
41.         } 
42.          
43.          
44.     } 
45. 
46. $dir=date('Y-m-d'); 
47. if(file_exists($dir)) 
48. { 
49. mrdirs($dir); 
50. } 
51. else
52. { 
53.     echo "文件夹不存在,无法删除"; 
54. } 
55. ?>
输出结果,文件夹已经删除
但此方法只适用于文件夹中有文件的,文件夹的嵌套将不起作用
作者“PHP学习笔记”

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478642.htmlTechArticle1.函数 file(目标文件) 把文件以数组的形式读出来,用循环方式遍历数组输 1.? 2.header(Content-Type:text/html; charset=utf-8); 3.$file=file(test.txt); 4.if(...
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!