php5&mysql 程式設計

30
PHP5&MySQL PHP5&MySQL 程程程程 程程程程 7 7 第第第第 第第第第

Upload: yanka

Post on 06-Jan-2016

43 views

Category:

Documents


2 download

DESCRIPTION

PHP5&MySQL 程式設計. 第 7 章 檔案存取. 7-1  存取路徑. 7-1-1  取得檔案名稱 basename(string path [, string suffix ]) \ch07\prac07-1.php 01: 02: 03:

TRANSCRIPT

Page 1: PHP5&MySQL 程式設計

PHP5&MySQLPHP5&MySQL 程式設計程式設計

第第 77 章 檔案存取章 檔案存取

Page 2: PHP5&MySQL 程式設計

7-17-1  存取路徑  存取路徑 7-1-17-1-1  取得檔案名稱  取得檔案名稱 basename(string basename(string pathpath [, string [, string suffixsuffix]) ])

\ch07\prac07-1.php\ch07\prac07-1.php01:<HTML>01:<HTML>02: <BODY>02: <BODY>03: <?php03: <?php04: $path = $_SERVER['PHP_SELF'];04: $path = $_SERVER['PHP_SELF'];05: echo 05: echo basename($path)basename($path).'<BR>';.'<BR>';06: echo 06: echo basename($path, '.php')basename($path, '.php').'<BR>';.'<BR>';07: ?>07: ?>08: </BODY>08: </BODY>09:</HTML> 09:</HTML>

Page 3: PHP5&MySQL 程式設計

7-1-27-1-2  取得路徑資訊  取得路徑資訊 pathinfo(string pathinfo(string pathpath) )

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

Page 4: PHP5&MySQL 程式設計

7-1-37-1-3  取得絕對路徑  取得絕對路徑 realpath(string realpath(string pathpath) )

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

Page 5: PHP5&MySQL 程式設計

7-27-2  存取伺服器端的資料夾  存取伺服器端的資料夾 7-2-17-2-1  建立資料夾  建立資料夾 mkdir(string mkdir(string pathnamepathname [, int [, int mode mode [, bool [, bool recursiverecursive]]) ]])

mkdir("c:\\myPHP\\pictures");mkdir("c:\\myPHP\\pictures");

mkdir("c:\\myPHP\\pictures", NULL, TRUE);mkdir("c:\\myPHP\\pictures", NULL, TRUE);

mkdir("pictures"); mkdir("pictures");

Page 6: PHP5&MySQL 程式設計

7-2-27-2-2  刪除資料夾  刪除資料夾 rmdir(string rmdir(string dirnamedirname) )

rmdir("C:\\myPHP\\pictures");rmdir("C:\\myPHP\\pictures");

Page 7: PHP5&MySQL 程式設計

7-2-37-2-3  判斷資料夾是否存在  判斷資料夾是否存在 file_exists(string file_exists(string filenamefilename) )

$folder_name = "C:\\myPHP\\pictures";$folder_name = "C:\\myPHP\\pictures";if (!if (!file_exists($folder_name)file_exists($folder_name))) mkdir($folder_name, NULL, TRUE);mkdir($folder_name, NULL, TRUE);elseelse echo "echo " 指定的資料夾已經存在指定的資料夾已經存在 "; ";

$folder_name = "C:\\myPHP\\pictures";$folder_name = "C:\\myPHP\\pictures";if (if (file_exists($folder_name)file_exists($folder_name))) rmdir($folder_name);rmdir($folder_name);elseelse echo "echo " 指定的資料夾不存在指定的資料夾不存在 "; ";

Page 8: PHP5&MySQL 程式設計

7-2-47-2-4  取得目前工作資料夾  取得目前工作資料夾 getcwd() getcwd()

7-2-57-2-5  切換目前工作資料夾  切換目前工作資料夾 chdir(string chdir(string directorydirectory) )

chdir("C:");chdir("C:");mkdir("mkdir(" 示範示範 "); ");

Page 9: PHP5&MySQL 程式設計

7-2-67-2-6  變更資料夾權限  變更資料夾權限 chmod(string chmod(string filenamefilename, int , int modemode) )

chmod("mydir", 0600);chmod("mydir", 0600);

7-2-77-2-7  判斷路徑是否為資料夾  判斷路徑是否為資料夾 is_dir(string is_dir(string filenamefilename) )

is_dir("C:") is_dir("C:")

7-2-87-2-8  取得資料夾的父資料夾名稱  取得資料夾的父資料夾名稱 dirname(string dirname(string pathpath) )

dirname("C:\\mysql") dirname("C:\\mysql")

Page 10: PHP5&MySQL 程式設計

7-2-97-2-9  使用 使用 readdir() readdir() 函式讀取資料夾內容函式讀取資料夾內容 一、開啟資料夾 一、開啟資料夾 opendir(string opendir(string pathpath) )

if ($handle = @if ($handle = @opendir("C:\\php")opendir("C:\\php"))) echo "echo " 開啟資料夾成功開啟資料夾成功 ";";elseelse echo "echo " 開啟資料夾失敗開啟資料夾失敗 "; ";

二、讀取資料夾內容二、讀取資料夾內容 readdir(resource readdir(resource dir_handledir_handle) )

三、關閉資料夾 三、關閉資料夾 closedir(resource closedir(resource dir_handledir_handle) )

Page 11: PHP5&MySQL 程式設計

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

echo "$file";echo "$file"; closedir($handle)closedir($handle); ; }} ?>?> </BODY></BODY></HTML> </HTML>

Page 12: PHP5&MySQL 程式設計

7-2-107-2-10  使用 使用 scandir() scandir() 函式讀取資料夾內容函式讀取資料夾內容 scandir(string scandir(string directorydirectory [, int [, int sorting_ordersorting_order]) ])

\ch07\prac07-6.php\ch07\prac07-6.php<HTML><HTML> <BODY><BODY> <?php<?php $file = $file = scandir("C:\\php", 1)scandir("C:\\php", 1);; foreach($file as $value)foreach($file as $value) if ($value != "." && $value != "..") echo $value;if ($value != "." && $value != "..") echo $value; ?>?> </BODY></BODY></HTML> </HTML>

Page 13: PHP5&MySQL 程式設計

7-37-3  存取伺服器端的檔案  存取伺服器端的檔案 7-3-17-3-1  判斷檔案是否存在  判斷檔案是否存在 file_exists(string file_exists(string filenamefilename) )

7-3-27-3-2  判斷指定的路徑是否為檔案  判斷指定的路徑是否為檔案 is_file(string is_file(string filenamefilename) )

7-3-37-3-3  複製檔案  複製檔案 copy(string copy(string sourcesource, string , string destdest) )

copy("C:\\php\\license.txt", "license (new).txt") copy("C:\\php\\license.txt", "license (new).txt")

Page 14: PHP5&MySQL 程式設計

7-3-47-3-4  刪除檔案  刪除檔案 unlink(string unlink(string filenamefilename) )

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

Page 15: PHP5&MySQL 程式設計

7-3-57-3-5  變更檔案名稱  變更檔案名稱 rename(string rename(string oldnameoldname, string , string newnamenewname) ) rename("temp.php", "temp.bak"); rename("temp.php", "temp.bak");

Page 16: PHP5&MySQL 程式設計

7-3-67-3-6  取得檔案屬性  取得檔案屬性 fileatime(string fileatime(string filenamefilename)) filectime(string filectime(string filenamefilename)) filemtime(string filemtime(string filenamefilename)) filesize(string filesize(string filenamefilename)) is_readable(string is_readable(string filenamefilename)) is_writable(string is_writable(string filenamefilename))

is_writeable(string is_writeable(string filenamefilename))

Page 17: PHP5&MySQL 程式設計

\ch07\prac07-8.php\ch07\prac07-8.php<HTML><HTML> <BODY><BODY> <?php<?php $filename = basename($_SERVER['PHP_SELF']);$filename = basename($_SERVER['PHP_SELF']); echo 'echo ' 目前網頁的建立時間:目前網頁的建立時間: '.date("Y-m-d H:i:s", '.date("Y-m-d H:i:s", filectime($filenamfilectime($filenam

e)e)).'<BR>';).'<BR>'; echo 'echo ' 目前網頁的最後存取時間:目前網頁的最後存取時間: '.date("Y-m-d H:i:s",'.date("Y-m-d H:i:s", fileatime($filen fileatime($filen

ameame)).'<BR>';)).'<BR>'; echo 'echo ' 目前網頁的修改時間:目前網頁的修改時間: '.date("Y-m-d H:i:s", '.date("Y-m-d H:i:s", filemtime($filenamfilemtime($filenam

e)e)).'<BR>';).'<BR>'; echo 'echo ' 目前網頁的檔案大小:目前網頁的檔案大小: '.'.filesize($filename)filesize($filename).'.' 位元組位元組 ';'; ?>?> </BODY></BODY></HTML> </HTML>

Page 18: PHP5&MySQL 程式設計

7-47-4  讀取伺服器端的文字檔案  讀取伺服器端的文字檔案 7-4-17-4-1  使用 使用 fread() fread() 函式讀取文字檔案 函式讀取文字檔案 一、開啟檔案 一、開啟檔案 fopen(string fopen(string filenamefilename, string , string modemode) ) 參數參數 modemode 可以指定的模式可以指定的模式

rr r+r+ ww w+w+ aa a+a+ xx x+x+

Page 19: PHP5&MySQL 程式設計

$handle = fopen("readme.txt", "w+b")$handle = fopen("readme.txt", "w+b")$handle = fopen("http://localhost/readme.txt", "w+b");$handle = fopen("http://localhost/readme.txt", "w+b");

$handle = fopen("http://localhost/readme.txt", "rb");$handle = fopen("http://localhost/readme.txt", "rb");

$handle = fopen("ftp://amily:[email protected]/readme.txt", "r+b");$handle = fopen("ftp://amily:[email protected]/readme.txt", "r+b");

$handle = fopen("http://localhost/readme.txt", "rb"); $handle = fopen("http://localhost/readme.txt", "rb");

Page 20: PHP5&MySQL 程式設計

二、讀取檔案 二、讀取檔案 fread(resource fread(resource handlehandle, int , int lengthlength))

$handle = fopen("poetry1.txt", "rb");$handle = fopen("poetry1.txt", "rb");fread($handle, filesize("poetry1.txt"))fread($handle, filesize("poetry1.txt"))

三、關閉檔案 三、關閉檔案 fclose(resource fclose(resource handlehandle) )

Page 21: PHP5&MySQL 程式設計

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

Page 22: PHP5&MySQL 程式設計

7-4-27-4-2  使用 使用 fgets() fgets() 函式讀取文字檔函式讀取文字檔案案

fgets(resource fgets(resource handlehandle) ) feof(resource feof(resource handlehandle) )

\ch07\prac07-10.php\ch07\prac07-10.php<HTML><HTML> <BODY><BODY> <?php<?php $handle = $handle = fopen("poetry1.txt", "rb")fopen("poetry1.txt", "rb");; while (while (!feof($handle)!feof($handle)) ) {{ $line = $line = nl2br(fgets($handle))nl2br(fgets($handle));; echo $line;echo $line; }} fclose($handle)fclose($handle);; ?>?> </BODY></BODY></HTML> </HTML>

Page 23: PHP5&MySQL 程式設計

7-4-37-4-3  使用 使用 fgetss() fgetss() 函式讀取文字檔函式讀取文字檔案案

fgetss(resource fgetss(resource handlehandle) )

\ch07\prac07-11.php\ch07\prac07-11.php<HTML><HTML> <BODY><BODY> <?php<?php $handle = $handle = fopen("poetry1.txt", "rb")fopen("poetry1.txt", "rb");; while (while (!feof($handle)!feof($handle)) ) {{ $line = $line = nl2br(fgetss($handle)nl2br(fgetss($handle));); echo $line;echo $line; }} fclose($handle)fclose($handle);; ?>?> </BODY></BODY></HTML> </HTML>

Page 24: PHP5&MySQL 程式設計

7-4-47-4-4  使用 使用 readfile() readfile() 函式讀取文字檔函式讀取文字檔案案

readfile(string readfile(string filenamefilename) )

\ch07\prac07-12.php\ch07\prac07-12.php<HTML><HTML> <BODY><BODY> <?php<?php readfile("poetry1.txt")readfile("poetry1.txt");; ?>?> </BODY></BODY></HTML> </HTML>

Page 25: PHP5&MySQL 程式設計

7-4-57-4-5  使用 使用 file_get_contents() file_get_contents() 函式讀取文字檔案函式讀取文字檔案 readfile(string readfile(string filenamefilename) )

\ch07\prac07-13.php\ch07\prac07-13.php

<HTML><HTML>

<BODY><BODY>

<?php<?php

echo echo nl2br(file_get_contents("poetry1.txt"))nl2br(file_get_contents("poetry1.txt"));;

?>?>

</BODY></BODY>

</HTML</HTML> >

Page 26: PHP5&MySQL 程式設計

7-4-67-4-6  使用 使用 file() file() 函式讀取文字檔函式讀取文字檔案 案

file(string file(string filenamefilename) )

\ch07\prac07-14.php\ch07\prac07-14.php<HTML><HTML> <BODY><BODY> <?php<?php $line = $line = file("poetry1.txt")file("poetry1.txt");; foreach($line as $data)foreach($line as $data) echo nl2br($data);echo nl2br($data); ?>?> </BODY></BODY></HTML> </HTML>

Page 27: PHP5&MySQL 程式設計

7-57-5  寫入伺服器端的文字檔案  寫入伺服器端的文字檔案 7-5-17-5-1  使用 使用 fwrite()fwrite() 、、 fputs() fputs() 函式寫入文字檔案函式寫入文字檔案fwrite(resource fwrite(resource handlehandle, string , string stringstring [, int [, int lengthlength))fputs(resource fputs(resource handlehandle, string , string stringstring [, int [, int lengthlength) )

假設假設 poetry2.txtpoetry2.txt 檔案的原始內容如下:檔案的原始內容如下: <I><I> 鳳凰臺上鳳凰遊,鳳去臺空江自流。鳳凰臺上鳳凰遊,鳳去臺空江自流。 </I></I> <I><I> 吳宮花草埋幽徑,晉代衣冠成古邱。吳宮花草埋幽徑,晉代衣冠成古邱。 </I></I>

我們打算在檔案的尾端加寫入如下資料,所以檔案模式必我們打算在檔案的尾端加寫入如下資料,所以檔案模式必須使用須使用 aa 或或 a+a+ : :

<I><I> 三山半落青又外,二水中分白鷺洲。三山半落青又外,二水中分白鷺洲。 </I></I> <I><I> 總為浮雲能蔽日,長安不見使人愁。總為浮雲能蔽日,長安不見使人愁。 </I> </I>

Page 28: PHP5&MySQL 程式設計

\ch07\prac07-15.php\ch07\prac07-15.php<HTML><HTML> <BODY><BODY> <?php<?php $handle = $handle = fopen("poetry2.txt", "ab")fopen("poetry2.txt", "ab");; if ($handle) if ($handle) {{ $contents .= "\r\n";$contents .= "\r\n";

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

Page 29: PHP5&MySQL 程式設計
Page 30: PHP5&MySQL 程式設計

7-5-27-5-2  使用 使用 file_put_contents() file_put_contents() 函式寫入文字檔案函式寫入文字檔案 file_put_contents(string file_put_contents(string filenamefilename, string , string datadata))\ch07\prac07-16.php\ch07\prac07-16.php<HTML><HTML> <BODY><BODY> <?php<?php $contents = "$contents = " 故人具雞黍,邀我至田家,故人具雞黍,邀我至田家, \r\n"; \r\n";

//// 指定要寫入檔案的內容指定要寫入檔案的內容 $contents .= "$contents .= " 綠樹村邊合,青山郭外斜,綠樹村邊合,青山郭外斜, \r\n";\r\n"; $contents .= "$contents .= " 開軒面場圃,把酒話桑麻,開軒面場圃,把酒話桑麻, \r\n";\r\n"; $contents .= "$contents .= " 待到重陽日,還來就菊花。待到重陽日,還來就菊花。 ";"; $num = $num = file_put_contents("poetry3.txt", $contents)file_put_contents("poetry3.txt", $contents);; echo "echo " 成功寫入成功寫入 ".$num."".$num." 個位元組個位元組 ";"; ?>?> </BODY></BODY></HTML> </HTML>