簡(jiǎn)介:本次介紹的是PHP中與文件操作相關(guān)的系統(tǒng)函數(shù),這些函數(shù)也是非常的重要,下面天涯依然是對(duì)比較常用的進(jìn)行詳細(xì)舉例說(shuō)明.

basename — 返回路徑中的文件名部分

dirname — 返回路徑中的目錄部分

string basename ( string $path [, string $suffix ] )

string dirname ( string $path )

示例,代碼如下:

  1. <?php 
  2.     //PHP博客 http://www.phpfensi.com 
  3.     $path = "/home/httpd/www.phpfensi.com/index.php"
  4.     echo basename($path); 
  5.     echo basename($path'.php'); 
  6.     echo basename($path'.xxx'); 
  7.     echo dirname($path); 
  8.     ?> 
  9.     //結(jié)果: 
  10.     index.php 
  11.     index 
  12.     index.php 
  13.     /home/httpd/www.phpfensi.com  

說(shuō)明:如果文件名是以正確的suffix結(jié)束的,那這一部分也會(huì)被去掉.

chgrp — 改變文件所屬的組

chown — 改變文件的所有者

chmod — 改變文件模式

bool chmod ( string $filename , int $mode )

示例,代碼如下:chmod('/home/phpha.txt', 0755);

copy,拷貝文件,代碼如下:

  1. if(copy('index.php''index.php.bak')){ 
  2.   echo 'copy success'
  3.   } 

在當(dāng)前目錄下生存了index.php.bak文件.

delete — 參見(jiàn) unlink 或 unset

unlink — 刪除文件

代碼如下:

  1. <?php 
  2. if(unlink('index.php.bak')){ 
  3. echo 'unlink success'
  4. ?> 

刪除了index.php.bak

disk_free_space — 返回目錄中的可用空間

disk_total_space — 返回一個(gè)目錄的磁盤(pán)總大小

diskfreespace — disk_free_space 的別名

代碼如下:

  1. <?php 
  2.   //在 Windows 下: 
  3.   echo disk_free_space("C:"), '<br />'
  4.   echo disk_total_space("C:"); 
  5.   ?> 
  6.   //結(jié)果:返回的是字節(jié)數(shù) 
  7.   17433419776 
  8.   32218386432  

fopen — 打開(kāi)文件或者 URL

fgets — 從文件指針中讀取一行

feof — 測(cè)試文件指針是否到了文件結(jié)束的位置

fread — 讀取文件(可安全用于二進(jìn)制文件)

fwrite — 寫(xiě)入文件(可安全用于二進(jìn)制文件)

fclose — 關(guān)閉一個(gè)已打開(kāi)的文件指針

代碼如下:

  1. <?php 
  2.     $fp = fopen('hello.txt''r'); //打開(kāi)一個(gè)文件 
  3.     $n = 1; 
  4.     while(!feof($fp)){ 
  5.     echo $n' - 'fgets($fp), '<br />'//讀取一行并輸出 
  6.     $n++; 
  7.     } 
  8.     fclose($fp); //關(guān)閉文件 
  9.     ?> 
  10.     //輸出: 
  11.     1 - Welcome to my blog: 
  12.     2 - http://www.phpfensi.com  

fgetc — 從文件指針中讀取字符

fgetcsv — 從文件指針中讀入一行并解析 CSV 字段

fgetss — 從文件指針中讀取一行并過(guò)濾掉 HTML 標(biāo)記

fputcsv — 將行格式化為 CSV 并寫(xiě)入文件指針

fputs — fwrite 的別名

代碼如下:

  1. <?php 
  2.     $fp = fopen('hello.txt''r'); 
  3.     while(false !== ($char = fgetc($fp))){ 
  4.     echo $char'-'
  5.     } 
  6.     ?> 
  7.     //輸出: 
  8.     W-e-l-c-o-m-e- -t-o- -m-y- -b-l-o-g-:- - -h-t-t-p-:-/-/-b-l-o-g-.-p-h-p-h-a-.-c-o-m-  

file_exists — 檢查文件或目錄是否存在,代碼如下:

  1. <?php 
  2.     if(file_exists('hello.txt')){ 
  3.     echo 'hello.txt exists'
  4.     }else
  5.     echo 'hello.txt not exists'
  6.     } 
  7.     ?> 
  8.     //輸出: 
  9.     hello.txt exists 

file_get_contents — 將整個(gè)文件讀入一個(gè)字符串

file_put_contents — 將一個(gè)字符串寫(xiě)入文件

file — 把整個(gè)文件讀入一個(gè)數(shù)組中

代碼如下:

  1. <?php 
  2.     if($content = file_get_contents('hello.txt')){ 
  3.     file_put_contents('hello.txt.bak'$content); 
  4.     } 
  5.     ?> 
  6.     //相當(dāng)于copy了一份hello.txt 
  7.     <?php 
  8.     if($content = file('hello.txt')){ 
  9.     print_r($content); 
  10.     } 
  11.     ?> 
  12.     //數(shù)組形式,每一行是一個(gè)數(shù)組成員 
  13.     Array 
  14.     ( 
  15.     [0] => Welcome to my blog: 
  16.     [1] => http://www.phpfensi.com 
  17.     )  

fileatime — 取得文件的上次訪問(wèn)時(shí)間

filectime — 取得文件的 inode 修改時(shí)間

filegroup — 取得文件的組

fileinode — 取得文件的 inode

filemtime — 取得文件修改時(shí)間

fileowner — 取得文件的所有者

fileperms — 取得文件的權(quán)限

filesize — 取得文件大小

filetype — 取得文件類型

代碼如下:

  1. <?php 
  2.     echo fileatime('hello.txt'); 
  3.     echo filectime('hello.txt'); 
  4.     echo filegroup('hello.txt'); 
  5.     echo filemtime('hello.txt'); 
  6.     echo fileowner('hello.txt'); 
  7.     echo substr(sprintf('%o'fileperms('hello.txt')), -4); 
  8.     echo filesize('hello.txt'); 
  9.     echo filetype('hello.txt'); 
  10.     ?> 
  11.     //輸出: 
  12.     1353329003 
  13.     1353329003 
  14.     0 
  15.     1353330002 
  16.     0 
  17.     0666 
  18.     42 
  19.     file  

flock — 輕便的咨詢文件鎖定

fnmatch — 用模式匹配文件名

fflush — 將緩沖內(nèi)容輸出到文件

fpassthru — 輸出文件指針處的所有剩余數(shù)據(jù)

fscanf — 從文件中格式化輸入

fseek — 在文件指針中定位

fstat — 通過(guò)已打開(kāi)的文件指針取得文件信息

ftell — 返回文件指針讀/寫(xiě)的位置

ftruncate — 將文件截?cái)嗟浇o定的長(zhǎng)度

glob — 尋找與模式匹配的文件路徑

is_dir — 判斷給定文件名是否是一個(gè)目錄

is_executable — 判斷給定文件名是否可執(zhí)行

is_file — 判斷給定文件名是否為一個(gè)正常的文件

is_link — 判斷給定文件名是否為一個(gè)符號(hào)連接

is_readable — 判斷給定文件名是否可讀

is_uploaded_file — 判斷文件是否是通過(guò) HTTP POST 上傳的

is_writable — 判斷給定的文件名是否可寫(xiě)

is_writeable — is_writable 的別名

說(shuō)明:以上函數(shù)都是用來(lái)判斷文件或目錄是否符合對(duì)應(yīng)的條件,返回TRUE或FALSE.

lchgrp — Changes group ownership of symlink

lchown — Changes user ownership of symlink

link — 建立一個(gè)硬連接

linkinfo — 獲取一個(gè)連接的信息

lstat — 給出一個(gè)文件或符號(hào)連接的信息

mkdir — 新建目錄

move_uploaded_file — 將上傳的文件移動(dòng)到新位置

parse_ini_file — 解析一個(gè)配置文件

pathinfo — 返回文件路徑的信息

pclose — 關(guān)閉進(jìn)程文件指針

popen — 打開(kāi)進(jìn)程文件指針

readfile — 輸出一個(gè)文件

readlink — 返回符號(hào)連接指向的目標(biāo)

realpath — 返回規(guī)范化的絕對(duì)路徑名

rename — 重命名一個(gè)文件或目錄

rewind — 倒回文件指針的位置

rmdir — 刪除目錄

set_file_buffer — stream_set_write_buffer 的別名

stat — 給出文件的信息

symlink — 建立符號(hào)連接

tempnam — 建立一個(gè)具有唯一文件名的文件

tmpfile — 建立一個(gè)臨時(shí)文件

touch — 設(shè)定文件的訪問(wèn)和修改時(shí)間

umask — 改變當(dāng)前的 umask

clearstatcache — 清除文件狀態(tài)緩存

總結(jié):其實(shí)這么多的文件操作函數(shù),大部分用不到,同時(shí)可以看到這些函數(shù)與linux命令相似度有多么大.

轉(zhuǎn)載請(qǐng)注明來(lái)源:php 文件系統(tǒng)函數(shù)整理介紹

  哈爾濱品用軟件有限公司致力于為哈爾濱的中小企業(yè)制作大氣、美觀的優(yōu)秀網(wǎng)站,并且能夠搭建符合百度排名規(guī)范的網(wǎng)站基底,使您的網(wǎng)站無(wú)需額外費(fèi)用,即可穩(wěn)步提升排名至首頁(yè)。歡迎體驗(yàn)最佳的哈爾濱網(wǎng)站建設(shè)。