Php基础文件操作
1、獲取文件名:
basename($filepath,$ext);$filepath 文件的全路徑字符串 $ext 擴(kuò)展名??
<pre name="code" class="php">$path = "/home/httpd/html/index.php"; $file = basename($path,".php"); 返回index 2、獲取文件路徑的目錄部分:dirname($filepath)? $filepath 文件的全路徑字符串
<pre name="code" class="php">$path = "/home/httpd/html/index.php"; $file = dirname($path); 返回/home/httpd/html 3、獲取文件路徑關(guān)聯(lián)數(shù)組pathinfo($filepath) $filepath 文件的全路徑字符串
$pathinfo = pathinfo("/home/httpd/html/index.php"); var_dump($pathinfo); 返回 array(4) { ["dirname"]=> string(16) "/home/httpd/html" ["basename"]=> string(9) "index.php" ["extension"]=> string(3) "php" ["filename"]=> string(5) "index"}/home/httpd/html$pathinfo['dirname']//文件路徑 $pathinfo['basename']//文件名稱帶擴(kuò)展名 $pathinfo['extension']//擴(kuò)展名 $pathinfo['filename']//文件名不帶擴(kuò)展名4、取得文件的類型
filetype($filename) 返回文件的類型,可能的值有fifo、char、dir、block、link、file。unknown
5、獲取指定文件有用信息數(shù)組
fstat() 獲取由文件指針handle所打開文件的統(tǒng)計(jì)信息。包含的文件數(shù)組信息如下:
| 0 | dev | 設(shè)備名 |
| 1 | ino | 號(hào)碼 |
| 2 | mode | inode 保護(hù)模式 |
| 3 | nlink | 被連接數(shù)目 |
| 4 | uid | 所有者的用戶 id |
| 5 | gid | 所有者的組 id |
| 6 | rdev | 設(shè)備類型,如果是 inode 設(shè)備的話 |
| 7 | size | 文件大小的字節(jié)數(shù) |
| 8 | atime | 上次訪問時(shí)間(Unix 時(shí)間戳) |
| 9 | mtime | 上次修改時(shí)間(Unix 時(shí)間戳) |
| 10 | ctime | 上次改變時(shí)間(Unix 時(shí)間戳) |
| 11 | blksize | 文件系統(tǒng) IO 的塊大小 |
| 12 | blocks | 所占據(jù)塊的數(shù)目 |
stat($filename) 根據(jù)文件名獲取文件信息數(shù)組,返回信息與fstat類似
注意:本函數(shù)的結(jié)果會(huì)被緩存。請(qǐng)使用 clearstatcache()來清除緩存;
6、目錄操作:
遍歷目錄:
mkdir();
刪除目錄:
rmdir();
檢查目錄是否存在
is_dir();
7、操作文件:
新建文件:
$filename="test.txt"; $fp=fopen("$filename", "w+"); //打開文件指針,創(chuàng)建文件 if ( !is_writable($filename) ){die("文件:" .$filename. "不可寫,請(qǐng)檢查!"); } fclose($fp); //關(guān)閉指針讀取文件:
if(is_readable("test.txt")) { <pre name="code" class="php"> //首先采用“fopen”函數(shù)打開文件,得到返回值的就是資源類型。? $file_handle = fopen("test.txt","r"); if ($file_handle){ //接著采用while循環(huán)(后面語言結(jié)構(gòu)語句中的循環(huán)結(jié)構(gòu)會(huì)詳細(xì)介紹)一行行地讀取文件,然后輸出每行的文字 while (!feof($file_handle)) { //判斷是否到最后一行 $line = fgets($file_handle); //讀取一行文本 echo $line; //輸出一行文本 echo "<br />"; //換行 } } fclose($file_handle);//關(guān)閉文件}else{ echo "文件不存在或者無法讀取! ";}在php5中可以采用 file_get_contents()方法,參數(shù)為:
| file | 必需。規(guī)定要寫入數(shù)據(jù)的文件。如果文件不存在,則創(chuàng)建一個(gè)新文件。 |
| flags | 可選。規(guī)定如何讀取文件。可能的值:
如果參數(shù)省略則默認(rèn)為FILE_BINARY |
| context | 上下文 |
| offset | 可選。起始讀位置,偏移。 |
| maxlen | 可選。讀入長度。 |
fopen 讀寫文件方式參數(shù):
‘r’ 只讀方式打開,將文件指針指向文件頭。
‘r+’ 讀寫方式打開,將文件指針指向文件頭。
‘w’ 寫入方式打開,將文件指針指向文件頭并將文件大小截為零。如果文件不存在則嘗試創(chuàng)建文件。
‘w+’ 讀寫方式打開,將文件指針指向文件頭并將文件大小截為零。如果文件不存在則嘗試創(chuàng)建文件。
‘a(chǎn)’ 寫入方式打開,將文件指針指向文件末尾。如果文件不存在則嘗試創(chuàng)建文件。
‘a(chǎn)+’ 讀寫方式打開,將文件指針指向文件末尾。如果文件不存在則嘗試創(chuàng)建文件。
寫入文件: if(is_writable(test1.txt)) {if(!$fso=fopen("test1.txt",'w')){$this->warns('無法打開緩存文件.');//trigger_error}if(!flock($fso,LOCK_EX)){//LOCK_NB,排它型鎖定$this->warns('無法鎖定緩存文件.');//trigger_error}if(!fwrite($fso,"asdasdasdsadsadl了win笨笨按時(shí)打算大")){//寫入字節(jié)流,serialize寫入其他格式$this->warns('無法寫入緩存文件.');//trigger_error}flock($fso,LOCK_UN);//釋放鎖定fclose($fso); } else {die("文件:" .$filename. "不可寫,請(qǐng)檢查!"); } 在PHP5中可采用file_put_contents()方法,參數(shù)如下: <pre name="code" class="php">注意:要往文件里面寫入大量的數(shù)據(jù),則推薦用fwrite,不要用file_put_contents。在高并發(fā)的請(qǐng)求中也建議用fwrite。 就只有一句話,且不需要任何其他靈活的事情時(shí)使用。
復(fù)制文件:
$file = 'test1.txt'; $newfile = 'test2.txt'; # 這個(gè)文件父文件夾必須能寫 if (file_exists($file) == false) {die ('文件不存在無法復(fù)制'); } $result = copy($file, $newfile); if ($result == false) {echo '復(fù)制成功'; }刪除文件:unlink()方法
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
- 上一篇: Php基础数组篇
- 下一篇: Php基础正则表达式篇