程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php中的filesystem文件系統函數介紹及使用示例

php中的filesystem文件系統函數介紹及使用示例

編輯:關於PHP編程

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

復制代碼 代碼如下:
string basename ( string $path [, string $suffix ] )
string dirname ( string $path )

示例:
復制代碼 代碼如下:
<?php
$path = "/home/httpd/phpha.com/index.php";
echo basename($path);
echo basename($path, '.php');
echo basename($path, '.xxx');
echo dirname($path);
?>

復制代碼 代碼如下:
//結果:
index.php
index
index.php
/home/httpd/phpha.com

說明:如果文件名是以正確的suffix結束的,那這一部分也會被去掉。

chgrp — 改變文件所屬的組
chown — 改變文件的所有者
chmod — 改變文件模式

復制代碼 代碼如下:
bool chmod ( string $filename , int $mode )

示例:

復制代碼 代碼如下:
<?php
chmod('/home/phpha.txt', 0755);
?>
copy — 拷貝文件

if(copy('index.php', 'index.php.bak')){
 echo 'copy success';
}
?>
//在當前目錄下生存了index.php.bak文件

delete — 參見 unlink 或 unset
unlink — 刪除文件

復制代碼 代碼如下:
<?php
if(unlink('index.php.bak')){
 echo 'unlink success';
}
?>
//刪除了index.php.bak

disk_free_space — 返回目錄中的可用空間
disk_total_space — 返回一個目錄的磁盤總大小
diskfreespace — disk_free_space 的別名

復制代碼 代碼如下:
<?php
//在 Windows 下:
echo disk_free_space("C:"), '<br />';
echo disk_total_space("C:");
?>

復制代碼 代碼如下:
//結果:返回的是字節數
17433419776
32218386432

fopen — 打開文件或者 URL
fgets — 從文件指針中讀取一行
feof — 測試文件指針是否到了文件結束的位置
fread — 讀取文件(可安全用於二進制文件)
fwrite — 寫入文件(可安全用於二進制文件)
fclose — 關閉一個已打開的文件指針

復制代碼 代碼如下:
<?php
$fp = fopen('hello.txt', 'r'); //打開一個文件
$n = 1;
while(!feof($fp)){
 echo $n, ' - ', fgets($fp), '<br />'; //讀取一行並輸出
 $n++;
}
fclose($fp); //關閉文件
?>

復制代碼 代碼如下:
//輸出:
1 - Welcome to my blog:
2 - http://www.jb51.net

fgetc — 從文件指針中讀取字符
fgetcsv — 從文件指針中讀入一行並解析 CSV 字段
fgetss — 從文件指針中讀取一行並過濾掉 HTML 標記
fputcsv — 將行格式化為 CSV 並寫入文件指針
fputs — fwrite 的別名

復制代碼 代碼如下:
<?php
$fp = fopen('hello.txt', 'r');
while(false !== ($char = fgetc($fp))){
    echo $char, '-';
}
?>

復制代碼 代碼如下:
//輸出:
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 — 檢查文件或目錄是否存在

復制代碼 代碼如下:
<?php
if(file_exists('hello.txt')){
 echo 'hello.txt exists';
}else{
 echo 'hello.txt not exists';
}
?>

[/code]
//輸出:
hello.txt exists
[/code]

file_get_contents — 將整個文件讀入一個字符串
file_put_contents — 將一個字符串寫入文件
file — 把整個文件讀入一個數組中

復制代碼 代碼如下:
<?php
if($content = file_get_contents('hello.txt')){
 file_put_contents('hello.txt.bak', $content);
}
?>
//相當於copy了一份hello.txt
<?php
if($content = file('hello.txt')){
 print_r($content);
}
?>
//數組形式,每一行是一個數組成員
Array
(
    [0] => Welcome to my blog:
    [1] => http://www.jb51.net
)

fileatime — 取得文件的上次訪問時間
filectime — 取得文件的 inode 修改時間
filegroup — 取得文件的組
fileinode — 取得文件的 inode
filemtime — 取得文件修改時間
fileowner — 取得文件的所有者
fileperms — 取得文件的權限
filesize — 取得文件大小
filetype — 取得文件類型

復制代碼 代碼如下:
<?php
echo fileatime('hello.txt');
echo filectime('hello.txt');
echo filegroup('hello.txt');
echo filemtime('hello.txt');
echo fileowner('hello.txt');
echo substr(sprintf('%o', fileperms('hello.txt')), -4);
echo filesize('hello.txt');
echo filetype('hello.txt');
?>

復制代碼 代碼如下:
//輸出:
1353329003
1353329003
0
1353330002
0
0666
42
file

flock — 輕便的咨詢文件鎖定
fnmatch — 用模式匹配文件名
fflush — 將緩沖內容輸出到文件
fpassthru — 輸出文件指針處的所有剩余數據
fscanf — 從文件中格式化輸入
fseek — 在文件指針中定位
fstat — 通過已打開的文件指針取得文件信息
ftell — 返回文件指針讀/寫的位置
ftruncate — 將文件截斷到給定的長度
glob — 尋找與模式匹配的文件路徑

is_dir — 判斷給定文件名是否是一個目錄
is_executable — 判斷給定文件名是否可執行
is_file — 判斷給定文件名是否為一個正常的文件
is_link — 判斷給定文件名是否為一個符號連接
is_readable — 判斷給定文件名是否可讀
is_uploaded_file — 判斷文件是否是通過 HTTP POST 上傳的
is_writable — 判斷給定的文件名是否可寫
is_writeable — is_writable 的別名
說明:以上函數都是用來判斷文件或目錄是否符合對應的條件,返回TRUE或FALSE。

lchgrp — Changes group ownership of symlink
lchown — Changes user ownership of symlink
link — 建立一個硬連接
linkinfo — 獲取一個連接的信息
lstat — 給出一個文件或符號連接的信息
mkdir — 新建目錄
move_uploaded_file — 將上傳的文件移動到新位置
parse_ini_file — 解析一個配置文件
pathinfo — 返回文件路徑的信息
pclose — 關閉進程文件指針
popen — 打開進程文件指針
readfile — 輸出一個文件
readlink — 返回符號連接指向的目標
realpath — 返回規范化的絕對路徑名
rename — 重命名一個文件或目錄
rewind — 倒回文件指針的位置
rmdir — 刪除目錄
set_file_buffer — stream_set_write_buffer 的別名
stat — 給出文件的信息
symlink — 建立符號連接
tempnam — 建立一個具有唯一文件名的文件
tmpfile — 建立一個臨時文件
touch — 設定文件的訪問和修改時間
umask — 改變當前的 umask
clearstatcache — 清除文件狀態緩存

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved