PHP常用函数之文件系统处理
檢測(cè)
-
檢查文件或目錄是否存在
file_exists () -
檢查給定文件名是否為一個(gè)存在的文件(存在、文件)
is_file ( string $filename ) -
檢查給定目錄名是否為一個(gè)存在的目錄(存在、目錄)
is_dir ( string $filename ) -
判斷給定的文件名或目錄名是否存在且可讀(存在、文件或目錄、可讀)
is_readable ( string $filename ) -
判斷給定的文件名或目錄名是否存在且可寫(xiě)(存在、文件或目錄、可寫(xiě))
is_writable ( string $filename )
路徑解析
-
解析文件名
basename ( string $path [, string $suffix ] ) //包含有指向一個(gè)文件的全路徑的字符串
-
解析目錄名
dirname ( string $path ) //包含有指向一個(gè)文件的全路徑的字符串
-
解析全路徑
pathinfo ( string $path [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] )
目錄操作
-
新建目錄
mkdir (); //創(chuàng)建目錄,第三個(gè)參數(shù)表示是否遞歸創(chuàng)建 -
刪除目錄
rmdir (); //只能刪除空目錄,非空目錄必須使用遞歸刪除function removeDirOrFile($path){if(is_file($path)){return unlink($path);}if(is_dir($path)){$dir_handle = opendir($path);while(false !== ($file = readdir($dir_handle))) {if($file === '.' || $file === '..') continue;$subPath = $path.DIRECTORY_SEPARATOR.$file;$fnname = __FUNCTION__;$fnname($subPath);}closedir($dir_handle);return rmdir($path);}return FALSE; } -
移動(dòng)/重命名目錄
rename ( string $oldname , string $newname [, resource $context ] );
-
獲取目錄內(nèi)容
opendir(); readdir(); closedir(); rewind();function readDirsTree($path,$deep=0){if(is_file($path)){exit(basename($path));}if(is_dir($path)){$dir_handle = opendir($path);while(false !== ($file = readdir($dir_handle))) {if($file === '.' || $file === '..') continue;echo str_repeat(' ',$deep*2).iconv('GB2312','UTF-8',$file).'<br/>';if(is_dir($path.DIRECTORY_SEPARATOR.$file)){$fnname = __FUNCTION__;$fnname($path.DIRECTORY_SEPARATOR.$file, $deep+1);}}closedir($dir_handle);}} -
復(fù)制目錄
function copyDir($dirFrom, $dirTo){if(is_dir($dirFrom)){if(!file_exists($dirTo)){mkdir($dirTo,0777,TRUE);}$dir_handle = opendir($dirFrom);while(false !== ($file = readdir($dir_handle))) {if($file === '.' || $file === '..') continue;$fromPath = $dirFrom.DIRECTORY_SEPARATOR.$file;$toPath = $dirTo.DIRECTORY_SEPARATOR.$file;if(is_file($fromPath)){copy($fromPath, $toPath);}if(is_dir($fromPath)){$fnname = __FUNCTION__;$fnname($fromPath, $toPath);}}closedir($dir_handle);return TRUE;}else{return FALSE;} }
文件操作
-
獲取文件大小
filesize ( string $filename );
-
刪除文件
unlink ( string $filename);
-
剪切/重命名文件
rename ( string $oldname , string $newname );
-
拷貝文件
copy ( string $source , string $dest );
-
寫(xiě)文件
file_put_contents ( string $filename , mixed $data [, int $flags = 0 ] );
一般寫(xiě)文件就直接使用這個(gè)函數(shù),里面其實(shí)也是依次調(diào)用fopen(),fwrite()以及 fclose() 功能。 -
讀文件
file_get_contents ( string $filename );
此函數(shù)只適合讀一些小文件(文件大小很小的),如果讀大文件,必須使用下面方法,否則內(nèi)存很容易溢出fopen ( string $filename , string $mode );
fread ( resource $handle , int $length ); //按字節(jié)數(shù)讀取
fgets ( resource $handle [, int $length ] ); //默認(rèn)長(zhǎng)度為1kb,按行讀取
fgetc ( resource $handle ); //按1個(gè)字節(jié)1個(gè)字節(jié)讀取
fclose ( resource $handle ); 獲取文件修改時(shí)間
filemtime ( string $filename ); //返回時(shí)間戳
編碼問(wèn)題
在windows下,獲取含有中文的目錄名或文件名時(shí),由于中文是GBK編碼,而項(xiàng)目是utf-8編碼,所以必須轉(zhuǎn)碼iconv('GBK','utf-8',$filename);
當(dāng)輸入的路徑含有中文,由于項(xiàng)目是utf-8,而系統(tǒng)文件名或目錄名都是GBK編碼,所以必須轉(zhuǎn)為iconv('utf-8','GBK',$path);
總結(jié)
以上是生活随笔為你收集整理的PHP常用函数之文件系统处理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 解决ftp上传connection re
- 下一篇: WindowsServer2012 R2