项目开发中免不了要在服务器上创建文件夹,比如上传图片时的目录,模板解析时的目录等。这不当前手下的项目就用到了这个,于是总结了几个循环创建目录的方法。
PHP;">
function myscandir1($path,&$arr) {
foreach (glob($path) as $file) {
if (is_dir($file)) {
myscandir1($file . '/*',$arr);
} else {
$arr[] = realpath($file);
}
}
}
?>
PHP;">
$dir_handle = dir($path);
while (($file = $dir_handle->read()) !== false) {
while (($file = $dir_handle->read()) !== false) {
$p = realpath($path . '/' . $file);
if ($file != "." && $file != "..") {
$arr[] = $p;
}
if (is_dir($p) && $file != "." && $file != "..") {
myscandir2($p,$arr);
}
}
}
?>
@H_404_3@方法三:使用opendir && readdir循环
PHP;">
$dir_handle = opendir($path);
while (($file = readdir($dir_handle)) !== false) {
while (($file = readdir($dir_handle)) !== false) {
$p = realpath($path . '/' . $file);
if ($file != "." && $file != "..") {
$arr[] = $p;
}
if (is_dir($p) && $file != "." && $file != "..") {
myscandir3($p,$arr);
}
}
}
?>
PHP;">
$dir_handle = scandir($path);
foreach ($dir_handle as $file) {
foreach ($dir_handle as $file) {
$p = realpath($path . '/' . $file);
if ($file != "." && $file != "..") {
$arr[] = $p;
}
if (is_dir($p) && $file != "." && $file != "..") {
myscandir4($p,$arr);
}
}
}
?>
PHP;">
$iterator = new DirectoryIterator($path);
foreach ($iterator as $fileinfo) {
foreach ($iterator as $fileinfo) {
$file = $fileinfo->getFilename();
$p = realpath($path . '/' . $file);
if (!$fileinfo->isDot()) {
$arr[] = $p;
}
if ($fileinfo->isDir() && !$fileinfo->isDot()) {
myscandir5($p,$arr);
}
}
}
?>
可以用xdebug测试运行时间
PHP;">
//需要安装xdebug
echo xdebug_time_index(),"\n";
?>
原文链接:https://www.f2er.com/php/18677.htmlecho xdebug_time_index(),"\n";
?>