PHP常用函数之文件系统处理
檢測
-
檢查文件或目錄是否存在
file_exists () -
檢查給定文件名是否為一個存在的文件(存在、文件)
is_file ( string $filename ) -
檢查給定目錄名是否為一個存在的目錄(存在、目錄)
is_dir ( string $filename ) -
判斷給定的文件名或目錄名是否存在且可讀(存在、文件或目錄、可讀)
is_readable ( string $filename ) -
判斷給定的文件名或目錄名是否存在且可寫(存在、文件或目錄、可寫)
is_writable ( string $filename )
路徑解析
-
解析文件名
basename ( string $path [, string $suffix ] ) //包含有指向一個文件的全路徑的字符串
-
解析目錄名
dirname ( string $path ) //包含有指向一個文件的全路徑的字符串
-
解析全路徑
pathinfo ( string $path [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] )
目錄操作
-
新建目錄
mkdir (); //創建目錄,第三個參數表示是否遞歸創建 -
刪除目錄
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; } -
移動/重命名目錄
rename ( string $oldname , string $newname [, resource $context ] );
-
獲取目錄內容
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);}} -
復制目錄
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 );
-
寫文件
file_put_contents ( string $filename , mixed $data [, int $flags = 0 ] );
一般寫文件就直接使用這個函數,里面其實也是依次調用fopen(),fwrite()以及 fclose() 功能。 -
讀文件
file_get_contents ( string $filename );
此函數只適合讀一些小文件(文件大小很小的),如果讀大文件,必須使用下面方法,否則內存很容易溢出fopen ( string $filename , string $mode );
fread ( resource $handle , int $length ); //按字節數讀取
fgets ( resource $handle [, int $length ] ); //默認長度為1kb,按行讀取
fgetc ( resource $handle ); //按1個字節1個字節讀取
fclose ( resource $handle ); 獲取文件修改時間
filemtime ( string $filename ); //返回時間戳
編碼問題
在windows下,獲取含有中文的目錄名或文件名時,由于中文是GBK編碼,而項目是utf-8編碼,所以必須轉碼iconv('GBK','utf-8',$filename);
當輸入的路徑含有中文,由于項目是utf-8,而系統文件名或目錄名都是GBK編碼,所以必須轉為iconv('utf-8','GBK',$path);
總結
以上是生活随笔為你收集整理的PHP常用函数之文件系统处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决ftp上传connection re
- 下一篇: openlayer 3 在layer上添