PHP檔案操作函數_PHP教程

來源:互聯網
上載者:User
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學習筆記”

http://www.bkjia.com/PHPjc/478642.htmlwww.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(...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.