php目錄函數
Ⅰ 寫一個php函數,可以遍歷目錄下面的所有文件及子文件夾並修改後綴名
可以用遞歸的方式,還有別的方式能實現
function my_dir($dir)
{
$files = array();
if (@$handle = opendir($dir)) {
while (($file = readdir($handle)) !== false) {
if ($file != ".." && $file != ".") {
if (is_dir($dir . "/" . $file)) {
$files[$file] = my_dir($dir . "/" . $file);
} else {
$files[] = $file;
}
}
}
closedir($handle);
return $files;
}
}
Ⅱ 關於使用php生成目錄的問題。求高手給出函數代碼
寫的有點多,但是應該是實現了,a文件夾的數量是沒有控制的
$path1=__DIR__;//一級目錄所在路徑
$dir_arr1=scandir($path1);//一級目錄所在路徑的所有文件和文件夾
$count1=getDirCount($dir_arr1,$path1);//一級目錄個數
$x=0;//1級目錄索引
if($count1==0){
mkdir('a0',0777);
}
if($count1>1){
$x=$count1-1;
}
$path2=$path1."/a".$x;//二級目錄所在路徑
$dir_arr2=scandir($path2);//二級目錄所在路徑的所有文件和文件夾
$count2=getDirCount($dir_arr2,$path2);//二級目錄個數
$y=0;//二級目錄索引
if($count2==0){
mkdir($path2.'/b0',0777);
}
if($count2>1){
$y=$count2-1;
}
$path3=$path1."/a".$x."/b".$y;//三級目錄所在路徑
$dir_arr3=scandir($path3);//三級目錄所在路徑的所有文件和文件夾
$count3=getDirCount($dir_arr3,$path3);//三級目錄個數
$z=0;//三級目錄索引
if($count3==0){
mkdir($path3.'/c0',0777);
}
if($count3>1){
$z=$count3-1;
}
$path4=$path1."/a".$x."/b".$y."/c".$z;
$dir_arr4=scandir($path4);//三級目錄所在路徑的所有文件和文件夾
print_r($dir_arr4);
$count4=getFileCount($dir_arr4,$path4);//獲取文件數量
//當c文件夾中的文件數量大於等於5個則需要創建下一個c文件夾
if($count4>=5){
//判斷c文件夾是不是已經有五個了
if($z>=4){
//如果c文件夾5個了,就需要創建下一個b文件夾
if($y>=4){
//如果b文件夾5個了,就創建下一個a文件夾和b0,c0
$path4=$path1.'/a'.($x+1).'/b0/c0';
mkdir($path4,0777,true);
}else{
//如果b文件夾不到5個就創建下一個b,和c0
$path4=$path2.'/b'.($y+1).'/c0';
mkdir($path4,0777,true);
}
}else{
//如果c文件夾還不到五個就創建c
$path4=$path3.'/c'.($z+1);
mkdir($path4,0777);
}
}
//往c文件夾里寫數據
file_put_contents($path4.'/'.time().'.txt','123');
functiongetDirCount($arr,$path){
$dir_count=0;
foreach($arras$val){
if($val!='.'&&$val!='..'&&is_dir($path."/".$val)){
$dir_count+=1;
}
}
return$dir_count;
}
functiongetFileCount($arr,$path){
$file_count=0;
foreach($arras$val){
if($val!='.'&&$val!='..'&&is_file($path."/".$val)){
$file_count+=1;
}
}
return$file_count;
}
Ⅲ php中的幾個判斷文件和目錄的函數 is
is_file("mydoc.txt")判斷文件名是否是合法的文件,目錄不行;
is_dir(" "),判斷目錄是否存在,文件名不行。mkdir(""),產生一個新的目錄。
file_exists(),檢查文件和目錄是否存在。目錄或文件名都行。
Ⅳ PHP的掃描目錄函數scandir(),這個地方沒有看懂,有知道的朋友嗎
is_dir()函數用於檢測是不是目錄!
如果只是寫$item, 那參數就只是個目錄或者文件名稱, 沒有指明目錄的具體路徑, 那is_dir永遠會返回false的
後面 首先是查找返回字元, substr, 第一個參數要查找的字元串, 第二個參數是查找的開始位置, 第三個參數是返回的長度, 結果就是 返回第一個字元,
那麼, 恭喜你, 回答正確,確實是檢查首字元不是圓點
Ⅳ php 目錄遍歷、刪除 函數的使用介紹
小編今天沒事寫了目錄想關的函數
包括
遍歷該文件夾下的文件,目錄子目錄
讀取當前文件下目錄和文件
刪除當前文件夾下的目錄子目錄以及文件
以上三個函數目前還不支持中文文件
中文目錄
復制代碼
代碼如下:
<?php
header("Content-type:text/html;charset=utf-8");
/**
*
讀取當前目錄下的文件和目錄
*
*
@param
string
$path
路徑
*
@return
array
所有滿足條件的文件
*/
function
tlist($path){
$path
=
iconv('utf-8',
'gbk',
$path);
if(!is_dir($path)){
throw
new
Exception($path."不是目錄");
}
$arr
=
array('dir'=>array(),'file'=>array());
$hd
=
opendir($path);
while(($file
=
readdir($hd))!==false){
if($file=="."||$file=="..")
{continue;}
if(is_dir($path."/".$file)){
$arr['dir'][]
=
iconv('gbk','utf-8',$file);
}else
if(is_file($path."/".$file)){
$arr['file'][]
=
iconv('gbk','utf-8',$file);
}
}
closedir($hd);
echo
"目錄有:".implode("<br
/>",$arr['dir'])."<br
/>";
echo
"文件有:".implode("<br
/>",$arr['file']);
}
/**
*
遍歷當前目錄下的文件和目錄以及子文件夾中目錄
*
*
@param
string
$path
路徑
*
@return
array
所有滿足條件的文件
*/
function
blist($path){
if(!is_dir(iconv("utf-8","gbk",$path))){
throw
new
Exception("文件夾".$path."不存在或者不是文件");
}
$arr
=
array();
$hd
=
opendir(iconv("utf-8","gbk",$path));
while(($file
=
readdir($hd))!==false){
if($file=="."||$file=="..")
{continue;}
$newpath=iconv('utf-8',
'gbk',
$path)
.'/'.$file;
if(is_dir($newpath)){
$arr[]
=
blist($path."/".$file);
}else
if(is_file($newpath)){
$arr[]
=
iconv('gbk','utf-8',$file);
}
}
closedir($hd);
return
$arr;
}
/**
*
刪除目錄下的文件以及子目錄
*
#param
string
$path
路徑
*
#return
string
刪除成功返回true
失敗返回false;
*/
function
dirDel($path){
if(!is_dir($path)){
throw
new
Exception($path."輸入的不是有效目錄");
}
$hand
=
opendir($path);
while(($file
=
readdir($hand))!==false){
if($file=="."||$file=="..")
continue;
if(is_dir($path."/".$file)){
dirDel($path."/".$file);
}else{
@unlink($path."/".$file);
}
}
closedir($hand);
@rmdir($path);
}
?>
Ⅵ php對目錄進行操作時,先用哪個函數對操作目錄進行是否存在的判斷
file_exists — 檢查文件或目錄是否存在
說明
bool file_exists ( string $filename )
檢查文件或目錄是否存在。
參數
filename
文件或目錄的路徑。
在 Windows 中要用 //computername/share/filename 或者 \\computername\share\filename 來檢查網路中的共享文件。
返回值
如果由 filename 指定的文件或目錄存在則返回 TRUE,否則返回 FALSE。
Note:
This function will return FALSE for symlinks pointing to non-existing files.
Warning
如果因為安全模式的限制而導致不能訪問文件的話,該函數會返回 FALSE。然而,可以使用 include 來包含,如果文件在 safe_mode_include_dir 所指定的目錄里。
Note:
The check is done using the real UID/GID instead of the effective one.
Note: 因為 PHP 的整數類型是有符號整型而且很多平台使用32位整型, 對2GB以上的文件,一些文件系統函數可能返回無法預期的結果 。
範例
Example #1 測試一個文件是否存在
<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo "文件 $filename 存在";
} else {
echo "文件 $filename 不存在";
}
?>
//以上內容來自官方PHP開發幫助文檔
Ⅶ php中有沒有一個刪除非空目錄的系統函數
有了這個小程序,PHPer就不用手動進行刪除計算機上的目錄文件了,在練習PHP目錄文件操作時就可以用這個函數,在這個基礎上還可以增加瀏覽文件夾目錄,然後進行刪除。 代碼一: 復制代碼 代碼如下: <?php function d_rmdir($dirname) { //刪除非空目錄 if(!is_dir($dirname)) { return false; } $handle = @opendir($dirname); while(($file = @readdir($handle)) !== false){ if($file != '上面收集的,還是手冊里的好 代碼三: 復制代碼 代碼如下: functionremoveDir($dirName) { if(!is_dir($dirName)) { returnfalse; } $handle=@opendir($dirName); while(($file=@readdir($handle))!==false) { if($file!='.'&&$file!='..') { $dir=$dirName.'/'.$file; is_dir($dir)?removeDir($dir):@unlink($dir); } } closedir($handle); returnrmdir($dirName); } ?>
Ⅷ php中哪個函數可以取得某目錄下的所有文件名
沒有這樣的函數,只能自己實現,下面是我常用的,這個不僅會獲取到當前目錄下的所有文件,而且還會遍歷所有子文件夾下的文件。
<?php
functionread_all_dir($dir)
{
$result=array();
$handle=opendir($dir);
if($handle)
{
while(($file=readdir($handle))!==false)
{
if($file!='.'&&$file!='..')
{
$cur_path=$dir.DIRECTORY_SEPARATOR.$file;
if(is_dir($cur_path))
{
$result['dir'][$cur_path]=read_all_dir($cur_path);
}
else
{
$result['file'][]=$cur_path;
}
}
}
closedir($handle);
}
return$result;
}
?>
Ⅸ PHP中,什麼函數可以統計一個目錄中共有多少個文件
$arr = scandir($dir);
$all = count($arr)-2;//所有文件總數除./和../
$php = count(preg_grep("/\.php$/", $arr));
$txt0 = $all - count(preg_grep("/\.txt$/", $arr));
echo '共有'.$all.'個文件,php文件'.$php.'個,非txt文件'.$txt0.'個';
希採納
Ⅹ php中目錄操作opendir()、readdir()及scandir()用法示例
本文實例講述了php中目錄操作opendir()、readdir()及scandir()用法。分享給大家供大家參考,具體如下:
opendir(path,context)若成功,則該函數返回一個目錄流,否則返回
false
以及一個
error。可以通過在函數名前加上
「@」
來隱藏
error
的輸出。
readdir()
函數返回由
opendir()
打開的目錄句柄中的條目。若成功,則該函數返回一個文件名,否則返回
false。
scandir()
函數返回一個數組,其中包含指定路徑中的文件和目錄。
若成功,則返回一個數組,若失敗,則返回
false。如果
directory
不是目錄,則返回布爾值
false
付上2段搜到的讀取目錄的代碼,親測有效
顯示目錄中文件名
//
打開目錄,然後讀取其內容
if
(is_dir($dir)){
if
($dh
=
opendir($dir)){
while
(($file
=
readdir($dh))
!==
false){
echo
"filename:"
.
$file
.
"<br>";
}
closedir($dh);
}
}
拷貝一個目錄的文件到另一個目錄
_dir($from_dir,$to_dir);
function
_dir($from_dir,$to_dir){
if(!is_dir($from_dir)){
return
false;
}
echo
"\r\n
from:",$from_dir,'---to',$to_dir;
$from_files
=
scandir($from_dir);
//如果不存在目標目錄,則嘗試創建
if(!file_exists($to_dir)){
@mkdir($to_dir);
}
if(!empty($from_files)){
foreach
($from_files
as
$file){
if($file
==
'.'
||
$file
==
'..'
){
continue;
}
if(is_dir($from_dir.'/'.$file)){//如果是目錄,則調用自身
_dir($from_dir.'/'.$file,$to_dir.'/'.$file);
}else{//直接到目標文件夾
($from_dir.'/'.$file,$to_dir.'/'.$file);
}
}
}
}
更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP目錄操作技巧匯總》、《php文件操作總結》、《PHP常用遍歷演算法與技巧總結》、《PHP數據結構與演算法教程》、《php程序設計演算法總結》、《PHP數組(Array)操作技巧大全》、《php字元串(string)用法總結》及《php常見資料庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
您可能感興趣的文章:使用PHP函數scandir排除特定目錄PHP獲取當前文件所在目錄
getcwd()函數php文件夾與文件目錄操作函數介紹php中判斷文件空目錄是否有讀寫許可權的函數代碼PHP解析目錄路徑的3個函數總結PHP使用glob函數遍歷目錄或文件夾的方法php
file_exists
檢查文件或目錄是否存在的函數PHP遍歷目錄函數opendir()、readdir()、closedir()、rewinddir()總結PHP刪除非空目錄的函數代碼小結PHP目錄函數實現創建、讀取目錄教程實例php使用scandir()函數掃描指定目錄下所有文件示例