PHP中文件讀、寫、刪的操作(PHP中對文件和目錄操作)
Jun 13, 2016 pm 12:01 PM
一:目錄操作
首先介紹的是一個從目錄讀取的函數(shù),opendir(),readdir(),closedir(),使用的時候是先打開文件句柄,而后迭代列出:
復制代碼 代碼如下:
$base_dir = "filelist/";
$fso = opendir($base_dir);
echo $base_dir."
" ;
while($flist=readdir($fso)){
echo $flist."
" ;
}
closedir($fso)
?>
這是講返回文件目錄下面的文件已經(jīng)目錄的程序(0文件將返回false).
有時候需要知道目錄的信息,可以使用dirname($path)和basename($path),分別返回路徑的目錄部分和文件名名稱部分,可用disk_free_space($path)返回看空間空余空間.
創(chuàng)建命令:
mkdir($path,0777)
,0777是權(quán)限碼,在非window下可用umask()函數(shù)設置.
rmdir($path)
將刪除路徑在$path的文件.
dir -- directory 類也是操作文件目錄的重要類,有3個方法,read,rewind,close,這是一個仿面向?qū)ο蟮念?它先使用的是打開文件句柄,然后用指針的方式讀取的.,這里看php手冊:
復制代碼 代碼如下:
$d = dir("/etc/php5");
echo "Handle: " . $d->handle . "\n";
echo "Path: " . $d->path . "\n";
while (false !== ($entry = $d->read())) {
echo $entry."\n";
}
$d->close();
?>
[code]
輸出:
Handle: Resource id #2
Path: /etc/php5
.
..
apache
cgi
cli
文件的屬性也非常重要,文件屬性包括創(chuàng)建時間,最后修改時間,所有者,文件組,類型,大小等
下面我們重點談文件操作.
二:文件操作
讀文件
首先是一個文件看能不能讀取(權(quán)限問題),或者存在不,我們可以用is_readable函數(shù)獲取信息.:
[code]
$file = 'dirlist.php';
if (is_readable($file) == false) {
die('文件不存在或者無法讀取');
} else {
echo '存在';
}
?>
判斷文件存在的函數(shù)還有file_exists(下面演示),但是這個顯然無is_readable全面.,當一個文件存在的話可以用
復制代碼 代碼如下:
$file = "filelist.php";
if (file_exists($file) == false) {
die('文件不存在');
}
$data = file_get_contents($file);
echo htmlentities($data);
?>
但是file_get_contents函數(shù)在較低版本上不支持,可以先創(chuàng)建文件的一個句柄,然后用指針讀取全部:
復制代碼 代碼如下:
$fso = fopen($cacheFile, 'r');
$data = fread($fso, filesize($cacheFile));
fclose($fso);
還有一種方式,可以讀取二進制的文件:
$data = implode('', file($file));
詳細學習PHP中對文件和目錄的操作方法
一:引論
在任何計算機設備中,文件是都是必須的對象,而在web編程中,文件的操作一直是web程序員的頭疼的地方,而,文件的操作在cms系統(tǒng)中這是必須的,非常有用的,我們經(jīng)常遇到生成文件目錄,文件(夾)編輯等操作,現(xiàn)在我把php中的這些函數(shù)做一詳細總結(jié)并實例示范如何使用.,關(guān)于對應的函數(shù)詳細介紹,請查閱php手冊.此處只總結(jié)重點.和需要注意的地方.(這在php手冊是沒有的.)
二:目錄操作
首先介紹的是一個從目錄讀取的函數(shù),opendir(),readdir(),closedir(),使用的時候是先打開文件句柄,而后迭代列出:
復制代碼 代碼如下:
$base_dir = "filelist/";
$fso = opendir($base_dir);
echo $base_dir."
" ;
while($flist=readdir($fso)){
echo $flist."
" ;
}
closedir($fso)
?>
這是講返回文件目錄下面的文件已經(jīng)目錄的程序(0文件將返回false).
有時候需要知道目錄的信息,可以使用dirname($path)和basename($path),分別返回路徑的目錄部分和文件名名稱部分,可用disk_free_space($path)返回看空間空余空間.
創(chuàng)建命令:
mkdir($path,0777),0777是權(quán)限碼,在非window下可用umask()函數(shù)設置.
rmdir($path)將刪除路徑在$path的文件.
dir -- directory 類也是操作文件目錄的重要類,有3個方法,read,rewind,close,這是一個仿面向?qū)ο蟮念?它先使用的是打開文件句柄,然后用指針的方式讀取的.,這里看php手冊:
復制代碼 代碼如下:
$d = dir("/etc/php5");
echo "Handle: " . $d->handle . "\n";
echo "Path: " . $d->path . "\n";
while (false !== ($entry = $d->read())) {
echo $entry."\n";
}
$d->close();
?>
輸出:
Handle: Resource id #2
Path: /etc/php5
.
..
apache
cgi
cli
文件的屬性也非常重要,文件屬性包括創(chuàng)建時間,最后修改時間,所有者,文件組,類型,大小等
下面我們重點談文件操作.
三:文件操作
讀文件
首先是一個文件看能不能讀取(權(quán)限問題),或者存在不,我們可以用is_readable函數(shù)獲取信息.:
復制代碼 代碼如下:
$file = 'dirlist.php';
if (is_readable($file) == false) {
die('文件不存在或者無法讀取');
} else {
echo '存在';
}
?>
判斷文件存在的函數(shù)還有file_exists(下面演示),但是這個顯然無is_readable全面.,當一個文件存在的話可以用
復制代碼 代碼如下:
$file = "filelist.php";
if (file_exists($file) == false) {
die('文件不存在');
}
$data = file_get_contents($file);
echo htmlentities($data);
?>
但是file_get_contents函數(shù)在較低版本上不支持,可以先創(chuàng)建文件的一個句柄,然后用指針讀取全部:
$fso = fopen($cacheFile, 'r');
$data = fread($fso, filesize($cacheFile));
fclose($fso);
還有一種方式,可以讀取二進制的文件:
$data = implode('', file($file));
寫文件
和讀取文件的方式一樣,先看看是不是能寫:
復制代碼 代碼如下:
$file = 'dirlist.php';
if (is_writable($file) == false) {
die("我是雞毛,我不能");
}
?>
能寫了的話可以使用file_put_contents函數(shù)寫入:
復制代碼 代碼如下:
$file = 'dirlist.php';
if (is_writable($file) == false) {
die('我是雞毛,我不能');
}
$data = '我是可鄙,我想要';
file_put_contents ($file, $data);
?>
file_put_contents函數(shù)在php5中新引進的函數(shù)(不知道存在的話用function_exists函數(shù)先判斷一下)低版本的php無法使用,可以使用如下方式:
$f = fopen($file, 'w');
fwrite($f, $data);
fclose($f);
替換之.
寫文件的時候有時候需要鎖定,然后寫:
復制代碼 代碼如下:
function cache_page($pageurl,$pagedata){
if(!$fso=fopen($pageurl,'w')){
$this->warns('無法打開緩存文件.');//trigger_error
return false;
}
if(!flock($fso,LOCK_EX)){//LOCK_NB,排它型鎖定
$this->warns('無法鎖定緩存文件.');//trigger_error
return false;
}
if(!fwrite($fso,$pagedata)){//寫入字節(jié)流,serialize寫入其他格式
$this->warns('無法寫入緩存文件.');//trigger_error
return false;
}
flock($fso,LOCK_UN);//釋放鎖定
fclose($fso);
return true;
}
復制,刪除文件
php刪除文件非常easy,用unlink函數(shù)簡單操作:
復制代碼 代碼如下:
$file = 'dirlist.php';
$result = @unlink ($file);
if ($result == false) {
echo '蚊子趕走了';
} else {
echo '無法趕走';
}
?>
即可.
復制文件也很容易:
復制代碼 代碼如下:
$file = 'yang.txt';
$newfile = 'ji.txt'; # 這個文件父文件夾必須能寫
if (file_exists($file) == false) {
die ('小樣沒上線,無法復制');
}
$result = copy($file, $newfile);
if ($result == false) {
echo '復制記憶ok';
}
?>
可以使用rename()函數(shù)重命名一個文件夾.其他操作都是這幾個函數(shù)組合一下就能實現(xiàn)的.
獲取文件屬性
我說幾個常見的函數(shù):
獲取最近修改時間:
復制代碼 代碼如下:
$file = 'test.txt';
echo date('r', filemtime($file));
?>
返回的說unix的時間戳,這在緩存技術(shù)常用.
相關(guān)的還有獲取上次被訪問的時間fileatime(),filectime()當文件的權(quán)限,所有者,所有組或其它 inode 中的元數(shù)據(jù)被更新時間,fileowner()函數(shù)返回文件所有者 $owner = posix_getpwuid(fileowner($file));(非window系統(tǒng)),ileperms()獲取文件的權(quán)限,
復制代碼 代碼如下:
$file = 'dirlist.php';
$perms = substr(sprintf('%o', fileperms($file)), -4);
echo $perms;
?>
filesize()返回文件大小的字節(jié)數(shù):
復制代碼 代碼如下:
// 輸出類似:somefile.txt: 1024 bytes
$filename = 'somefile.txt';
echo $filename . ': ' . filesize($filename) . ' bytes';
?>
獲取文件的全部信息有個返回數(shù)組的函數(shù)stat()函數(shù):
復制代碼 代碼如下:
$file = 'dirlist.php';
$perms = stat($file);
var_dump($perms);
?>
那個鍵對應什么可以查閱詳細資料,此處不再展開.
四:結(jié)束語
上面我簡要的總結(jié)了一下幾個文件操作,如果您熟練掌握以上列出的函數(shù),已經(jīng)在操作的時候沒什么大的問題,php文件操作的函數(shù)變化比較快,現(xiàn)在已經(jīng)非常強大了,文件這部分也是學習php非常重要的一部分,希望不要忽略.

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

??? ??











phphasthreecommentstyles : //, #forsingle-lineand/.../formulti-lline.usecommentstoexplainwhycodeexists, notwhatitdoes.marktodo/fixMeitemsandDisableCodeTemporlinlyDuingDeBugging.aVoidOver-commentingsimplOgic.writeCoCoCoCoCoConcomeCOCOCOCONCOCOCOCOCOCOCOCOCISE

Windows? PHP? ???? ?? ???? ??? ?????. 1. ??? PHP ??? ?????? ?? ??????. Apache? ?? ThreadSafe ??? ????? Nginx??? ThreadSafe ??? ???? ?? ????. 2. php.ini ??? ???? php.ini-development ?? php.ini-production? php.ini? ?????. 3. ?? ? ??? ?? ??? ?? ?? ??? PHP ??? ??????. 4. PHP? ????? ?????? ??? ????? ?? ?? ?? PHP-V? ???? ?? ??? ???? ?? ?? ??? ??????. 5. Apache? ???? ?? httpd.conf?? p? ???????.

? ?? PHP ???? ??? ???? ??? ?????? ?? ?? ?? ??? ???? XAMPP/MAMP/LAMP? ?? ? ?? ??? ???? ???? ??? ???? ??? ?????. ??, hello.php?? ??? ??? ?? ??? ???? ???? ??????. ??, PHP ? HTML? ???? ?? ??? ??? ???? ?? ????. ?????, ???? ??, ?? ?? ? ?? ?? ??? ?? ???? ?????? ???? ???? ?? ????? ???????.

phpisaserver-sideScriptingLanguageUsedForWebDevelopment, ?? ProcessesData, InteractSwithDatabases ? SendShtmlTobrowsers.commonusesincludeusera-sectentication, e-commerceplatforms

ToHandleFileOperationsInphp, useAppreptFunctionsandModes.1.TOREADAFILE, USEFILE_GET_CONTENTS () FORSMALLFILESORFGETS () inALOOPFORLE-by-lineProcessing.2.TOWRITETOAFILE, USEFILE_PUTE_CONTENTS () USEFILE_PUTE_CONTENTS () FORSIMPLEWRITE () FORSIMPLAGFILE (ORFENDFLAG)

PHP? ?? ???? 4 ?? ?? ??? ?????. 1. PHP ??? ?????? ??? ??? ???? ?? ?????. 2. Echo ? Print? ????? ?? ???? ????, ??? Echo? ?? ?? ??? ?????? ??????. 3. ?? ???? ?? ???? ??????? //, # ? //; 4. ? ??? ?????? ????? ??? ?? ????? ??? ??? ??? ?? ???? ??? ????. ??? ?? ??? ????? ???? ???? PHP ??? ???? ? ??? ? ? ????.

???? PHP8? ???? ??? ??? ????. 1. ????? ??? ?? ????; 2. PHP8 ? ?? ?? ??? ??????. 3. ??? ????? ????? ??? ??????. 4. ??? ?? ?? ??? ??????. Windows ???? Zip ???? ?????? ?? ?? ? ?? ?? ??? ???? ???? ????? ?? ??? ??? ?? ? ? ????. MACOS ???? Homebrew? ???? ? ??, PHP8 ??, ?? ?? ?? ? ?? ??? ?? ??? ???? ?? ????. ?? ??? ?? ????? ???? ????? ????? ??? ?? ??? ??? ??? ? ????.

Python? Ifelse ??? ???? ??? ??? ??? ?? ??? ???? ????. 1. ???? ??? ???? ??? ???? ????. ??? ??? ?? ??? ???? ?? ?????. 2. ?? ?? ??? ELIF? ?? ????, ????? ???? ?? ???? ?????. 3. ?? ?? ??? ???? ?? ?? ? ?? ? ?? ???? ?? ?? ????. 4. 3 ?? ??? ??? ?????? ??? ifelse? ???? ? ??? ? ????. ?? ??, ??? ?? ? ??? ??????? ?????? ??? ???? ???? ?? ??? ??? ? ????.
