我的php脚本从db(id,name,path,status,tags,date)获取数据,然后php扫描项目目录中的所有html,php,css和js文件,并将它们分类为4个数组。之后,它格式化输出。 在sortFiles函数中,它检查是否有文件夹,如果它检测到文件夹,则调用相同的函数。它工作正常,但在第一个文件夹后,php不会跳回到第一个foreach循环然后完成...这是我的代码:
function sortFiles($dir,$name){
foreach ($dir as $file) {
$tmp = explode(".", $file);
if($tmp[1] === "html" || $tmp[1] === "css" || $tmp[1] === "js" || $tmp[1] === "php"){
$tmpArray = array();
$url = "http://www.nimmi.de/" . $GLOBALS["entry"][1] . "/" . $name . $file;
//$GLOBALS["output"] .= "NAME: " . $name;
array_push($tmpArray,$name . $file);
array_push($tmpArray,$url);
switch ($tmp[1]) {
case 'html':
array_push($GLOBALS["html"],$tmpArray);
break;
case 'php':
array_push($GLOBALS["php"],$tmpArray);
break;
case 'css':
array_push($GLOBALS["css"],$tmpArray);
break;
case 'js':
array_push($GLOBALS["js"],$tmpArray);
break;
default:
die("Unknown filetype");
break;
}
}
else if(strpos($file, '.') === false) {
$GLOBALS["paths"] .= $file . "/";
$GLOBALS["dirpath"] .= "/" . $file;
$subdir = scandir($GLOBALS["dirpath"]);
sortFiles($subdir,$GLOBALS["paths"]);
}
}
}
有人知道这个问题吗?
答案 0 :(得分:0)
解决方案(感谢Ryan Vincent):
function sortFiles($dir,$name){//$dir --> directory to query, $name --> subfolder names
foreach ($dir as $file) {//iterate through all files and sort them
$tmp = explode(".", $file);//splitts the file string into the name and the extension
if($tmp[1] === "html" || $tmp[1] === "css" || $tmp[1] === "js" || $tmp[1] === "php"){//checks the extension
$tmpArray = array();//wrapper array
$url = "../" . $GLOBALS["entry"][1] . "/" . $name . $file;//assemble the $url
array_push($tmpArray,$name . $file);//push the $name into the wrapper array
array_push($tmpArray,$url);//push the $url into the wrapper array
//push the wrapper into the specific extension array
switch ($tmp[1]) {
case 'html':
array_push($GLOBALS["html"],$tmpArray);
break;
case 'php':
array_push($GLOBALS["php"],$tmpArray);
break;
case 'css':
array_push($GLOBALS["css"],$tmpArray);
break;
case 'js':
array_push($GLOBALS["js"],$tmpArray);
break;
default:
die("Unknown filetype");
break;
}
}
else if(strpos($file, '.') === false) {//checks the $file if it is an folder
array_push($GLOBALS["dirpath"], "/" . $file);//adds the subfolder to the directory
$pathstr = implode("", $GLOBALS["dirpath"]);//unite the array to a string
$subdir = scandir($pathstr);//scans the directory
$dirname = explode($GLOBALS["dirpath"][0], $pathstr);//get the subfolder path as string
sortFiles($subdir,$dirname[1] . "/");//call this function again to sort all files in the subfolder
array_pop($GLOBALS["dirpath"]);//removes the last directory
}
}
}
答案 1 :(得分:-1)
尝试不使用此行
die("Unknown filetype");
这可能会破坏嵌套循环。