1 / 30

PHP5&MySQL 程式設計

PHP5&MySQL 程式設計. 第 7 章 檔案存取. 7-1  存取路徑. 7-1-1  取得檔案名稱 basename(string path [, string suffix ]) ch07prac07-1.php 01:<HTML> 02: <BODY> 03: <?php 04: $path = $_SERVER['PHP_SELF']; 05: echo basename($path) .'<BR>'; 06: echo basename($path, '.php') .'<BR>';

yanka
Download Presentation

PHP5&MySQL 程式設計

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP5&MySQL程式設計 第7章 檔案存取

  2. 7-1 存取路徑 7-1-1 取得檔案名稱 basename(string path [, string suffix]) \ch07\prac07-1.php 01:<HTML> 02: <BODY> 03: <?php 04: $path = $_SERVER['PHP_SELF']; 05: echo basename($path).'<BR>'; 06: echo basename($path, '.php').'<BR>'; 07: ?> 08: </BODY> 09:</HTML>

  3. 7-1-2 取得路徑資訊 pathinfo(string path) \ch07\prac07-2.php <HTML> <BODY> <?php $path = $_SERVER['PHP_SELF']; $path_parts = pathinfo($path); echo '目前網頁的路徑:'.$path.'<BR>'; echo '分割出來的路徑名稱:'.$path_parts['dirname'].'<BR>'; echo '分割出來的檔案名稱:'.$path_parts['basename'].'<BR>'; echo '分割出來的副檔名:'.$path_parts['extension'].'<BR>'; ?> </BODY> </HTML>

  4. 7-1-3 取得絕對路徑 realpath(string path) \ch07\prac07-3.php <HTML> <BODY> <?php $filename = basename($_SERVER['PHP_SELF']); echo '目前網頁的絕對路徑:'.realpath($filename); ?> </BODY> </HTML>

  5. 7-2 存取伺服器端的資料夾 7-2-1 建立資料夾 mkdir(string pathname [, int mode [, bool recursive]]) mkdir("c:\\myPHP\\pictures"); mkdir("c:\\myPHP\\pictures", NULL, TRUE); mkdir("pictures");

  6. 7-2-2 刪除資料夾 rmdir(string dirname) rmdir("C:\\myPHP\\pictures");

  7. 7-2-3 判斷資料夾是否存在 file_exists(string filename) $folder_name = "C:\\myPHP\\pictures"; if (!file_exists($folder_name)) mkdir($folder_name, NULL, TRUE); else echo "指定的資料夾已經存在"; $folder_name = "C:\\myPHP\\pictures"; if (file_exists($folder_name)) rmdir($folder_name); else echo "指定的資料夾不存在";

  8. 7-2-4 取得目前工作資料夾 getcwd() 7-2-5 切換目前工作資料夾 chdir(string directory) chdir("C:"); mkdir("示範");

  9. 7-2-6 變更資料夾權限 chmod(string filename, int mode) chmod("mydir", 0600); 7-2-7 判斷路徑是否為資料夾 is_dir(string filename) is_dir("C:") 7-2-8 取得資料夾的父資料夾名稱 dirname(string path) dirname("C:\\mysql")

  10. 7-2-9 使用readdir() 函式讀取資料夾內容 一、開啟資料夾 opendir(string path) if ($handle = @opendir("C:\\php")) echo "開啟資料夾成功"; else echo "開啟資料夾失敗"; 二、讀取資料夾內容 readdir(resource dir_handle) 三、關閉資料夾 closedir(resource dir_handle)

  11. \ch07\prac07-4.php <HTML> <BODY> <?php if ($handle = opendir("C:\\php")) { while (($file = readdir($handle)) != FALSE) echo "$file"; closedir($handle); } ?> </BODY> </HTML>

  12. 7-2-10 使用scandir() 函式讀取資料夾內容 scandir(string directory [, int sorting_order]) \ch07\prac07-6.php <HTML> <BODY> <?php $file = scandir("C:\\php", 1); foreach($file as $value) if ($value != "." && $value != "..") echo $value; ?> </BODY> </HTML>

  13. 7-3 存取伺服器端的檔案 7-3-1 判斷檔案是否存在 file_exists(string filename) 7-3-2 判斷指定的路徑是否為檔案 is_file(string filename) 7-3-3 複製檔案 copy(string source, string dest) copy("C:\\php\\license.txt", "license (new).txt")

  14. 7-3-4 刪除檔案 unlink(string filename) \ch07\prac07-7.php <HTML> <BODY> <?php $filename = "license (new).txt"; if (file_exists($filename)) { unlink($filename); echo "刪除檔案成功!"; } else { echo "檔案不存在,刪除檔案失敗!"; } ?> </BODY> </HTML>

  15. 7-3-5 變更檔案名稱 • rename(string oldname, string newname) • rename("temp.php", "temp.bak");

  16. 7-3-6 取得檔案屬性 • fileatime(string filename) • filectime(string filename) • filemtime(string filename) • filesize(string filename) • is_readable(string filename) • is_writable(string filename)is_writeable(string filename)

  17. \ch07\prac07-8.php <HTML> <BODY> <?php $filename = basename($_SERVER['PHP_SELF']); echo '目前網頁的建立時間:'.date("Y-m-d H:i:s", filectime($filename)).'<BR>'; echo '目前網頁的最後存取時間:'.date("Y-m-d H:i:s", fileatime($filename)).'<BR>'; echo '目前網頁的修改時間:'.date("Y-m-d H:i:s", filemtime($filename)).'<BR>'; echo '目前網頁的檔案大小:'.filesize($filename).'位元組'; ?> </BODY> </HTML>

  18. 7-4 讀取伺服器端的文字檔案 7-4-1 使用fread() 函式讀取文字檔案 一、開啟檔案 fopen(string filename, string mode) • 參數mode可以指定的模式 • r • r+ • w • w+ • a • a+ • x • x+

  19. $handle = fopen("readme.txt", "w+b") $handle = fopen("http://localhost/readme.txt", "w+b"); $handle = fopen("http://localhost/readme.txt", "rb"); $handle = fopen("ftp://amily:mypassword@ftp.asus.com.tw/readme.txt", "r+b"); $handle = fopen("http://localhost/readme.txt", "rb");

  20. 二、讀取檔案 fread(resource handle, int length) $handle = fopen("poetry1.txt", "rb"); fread($handle, filesize("poetry1.txt")) 三、關閉檔案 fclose(resource handle)

  21. \ch07\prac07-9.php <HTML> <BODY> <?php $filename = "poetry1.txt"; $handle = fopen($filename, "rb"); if ($handle) { $contents = nl2br(fread($handle, filesize($filename))); fclose($handle); echo $contents; } else echo "開啟檔案失敗!"; ?> </BODY> </HTML>

  22. 7-4-2 使用fgets() 函式讀取文字檔案 fgets(resource handle) feof(resource handle) \ch07\prac07-10.php <HTML> <BODY> <?php $handle = fopen("poetry1.txt", "rb"); while (!feof($handle)) { $line = nl2br(fgets($handle)); echo $line; } fclose($handle); ?> </BODY> </HTML>

  23. 7-4-3 使用fgetss() 函式讀取文字檔案 fgetss(resource handle) \ch07\prac07-11.php <HTML> <BODY> <?php $handle = fopen("poetry1.txt", "rb"); while (!feof($handle)) { $line = nl2br(fgetss($handle)); echo $line; } fclose($handle); ?> </BODY> </HTML>

  24. 7-4-4 使用readfile() 函式讀取文字檔案 readfile(string filename) \ch07\prac07-12.php <HTML> <BODY> <?php readfile("poetry1.txt"); ?> </BODY> </HTML>

  25. 7-4-5 使用file_get_contents() 函式讀取文字檔案 readfile(string filename) \ch07\prac07-13.php <HTML> <BODY> <?php echo nl2br(file_get_contents("poetry1.txt")); ?> </BODY> </HTML>

  26. 7-4-6 使用file() 函式讀取文字檔案 file(string filename) \ch07\prac07-14.php <HTML> <BODY> <?php $line = file("poetry1.txt"); foreach($line as $data) echo nl2br($data); ?> </BODY> </HTML>

  27. 7-5 寫入伺服器端的文字檔案 7-5-1 使用fwrite()、fputs() 函式寫入文字檔案 fwrite(resource handle, string string [, int length) fputs(resource handle, string string [, int length) 假設poetry2.txt檔案的原始內容如下: <I>鳳凰臺上鳳凰遊,鳳去臺空江自流。</I> <I>吳宮花草埋幽徑,晉代衣冠成古邱。</I> 我們打算在檔案的尾端加寫入如下資料,所以檔案模式必須使用a或a+: <I>三山半落青又外,二水中分白鷺洲。</I> <I>總為浮雲能蔽日,長安不見使人愁。</I>

  28. \ch07\prac07-15.php <HTML> <BODY> <?php $handle = fopen("poetry2.txt", "ab"); if ($handle) { $contents .= "\r\n"; //指定寫入檔案的內容,包括換行符號 $contents .= "<I>三山半落青又外,二水中分白鷺洲。<\I>\r\n"; $contents .= "<I>總為浮雲能蔽日,長安不見使人愁。<\I>"; $num = fwrite($handle, $contents); fclose($handle); echo "成功寫入".$num."個位元組"; } else echo "開啟檔案失敗"; ?> </BODY> </HTML>

  29. 7-5-2 使用file_put_contents() 函式寫入文字檔案 file_put_contents(string filename, string data) \ch07\prac07-16.php <HTML> <BODY> <?php $contents = "故人具雞黍,邀我至田家,\r\n"; //指定要寫入檔案的內容 $contents .= "綠樹村邊合,青山郭外斜,\r\n"; $contents .= "開軒面場圃,把酒話桑麻,\r\n"; $contents .= "待到重陽日,還來就菊花。"; $num = file_put_contents("poetry3.txt", $contents); echo "成功寫入".$num."個位元組"; ?> </BODY> </HTML>

More Related