為啥報(bào)錯(cuò),說$arr未定義(return $arr;)?
<?php
//打開指定目錄
/**
?* 遍歷目錄函數(shù),只讀取目錄中的最外層的內(nèi)容
?* @param string $path
?* @return array
?*/
function readDirectory($path){
$handle=opendir($path); ? //打開指定目錄
while($item=readdir($handle)!==false){
//.和..2個(gè)特殊目錄,.表示當(dāng)前目錄,..表示上級(jí)目錄
if(($item!=".") && ($item!="..")){
if(is_file($path."/".$item)){
$arr['file'][]=$item;
}
if(is_dir($path."/".$item)){
$arr['dir'][]=$item;
}
else {echo "empty";}
}
}
closedir($handle);
return $arr;
}
$path="file";
print_r(readDirectory($path));
2015-07-13
因?yàn)槔ㄌ?hào)的問題 ?你存儲(chǔ)到 ?$arr里的 數(shù)據(jù)全是 boolean ?類型的數(shù)據(jù) ?導(dǎo)致報(bào)錯(cuò)了
2016-11-28
在函數(shù)開頭定義個(gè)$arr=[];就算file目錄下無(wú)文件都可以返回個(gè)空數(shù)組,不會(huì)報(bào)錯(cuò).
2015-07-13
你代碼寫錯(cuò)了while (($pathname = readdir($handle)) !== false)
2015-07-12
想問你一下,$arr['file'][]=$item;這里面的$arr數(shù)組的定義在哪里呢?
2015-07-11
<?php
//打開指定目錄
/**
?* 遍歷目錄函數(shù),只讀取目錄中的最外層的內(nèi)容
?* @param string $path
?* @return array
?*/
function readDirectory($path){
$handle=opendir($path); ? //打開指定目錄
while($item=readdir($handle)!==false){
//.和..2個(gè)特殊目錄,.表示當(dāng)前目錄,..表示上級(jí)目錄
if(($item!=".") && ($item!="..")){
if(is_file($path."/".$item)){
$arr['file'][]=$item;
}
if(is_dir($path."/".$item)){
$arr['dir'][]=$item;
}
else {echo "empty";}
}
}
closedir($handle);
return $arr;
}
$path="file";
print_r(readDirectory($path));
2015-07-11
你把代碼貼出來(lái)給我們看看撒!以后這樣的問題,先把代碼貼出來(lái)再問呢。