如果1
3
中有2个文件夹名称./path/1
和./path/3
,现在我尝试添加新文件夹,但是如何对文件夹名称进行排序已经存在并找到丢失的号码是2
?
<?php
$file = 0;
$folder = 0;
$dir = new RecursiveDirectoryIterator('./img/product/tmp', FilesystemIterator::SKIP_DOTS);
$it = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST);
$it->setMaxDepth(0);
foreach ($it as $fileinfo) {
if ($fileinfo->isDir()) {
printf("Folder - %s\n", $fileinfo->getFilename());
$folder++;
} elseif ($fileinfo->isFile()) {
printf("File From %s - %s\n", $it->getSubPath(), $fileinfo->getFilename());
$file++;
}
}
if(/* find the missing number */){
$folder_new = //missing number
$dir = './path/'.$folder_new;
if(!is_dir($dir)){
mkdir($dir);
}else{
$folder_new = $folder+1;
$dir = './path/'.$folder_new;
if(!is_dir($dir)){
mkdir($dir);
}
}
?>
答案 0 :(得分:1)
basicly:
$root = $_SERVER['DOCUMENT_ROOT']; // '.' doesn't work on the backend for going to the root.
$path = "$root/path/";
$dirs = glob("$path*"); // this creates an array with everything inside $path
sort($dirs); //you wanted the directories sorted
//deleting the files (not dirs)
foreach($dirs as $k => $dir){
if(!is_dir($dir)){
unset($dirs[$k]);
}
}
$max = array_max($dirs); //folder number with the highest number as name.
// this is the part finding out what number is missing
for($i = 0; $i <= $max; $i++){
if(!is_dir($path.$i){
mkdir($path.$i);
}
}
没有测试过,只是把它写出来,希望它适合你:)
答案 1 :(得分:0)
我原以为如果你能得到目录中的文件夹列表(./path), 然后你可以比较看看存在哪些目录:
for ( $i = 0; $i < 100; $i++ ) {
if ( ! is_dir( './path/' . $i ) ) {
mkdir( './path/' . $i );
}
}
答案 2 :(得分:0)
<?php
$to = 5;
for ($i = 1; $i <= $to; $i++) {
echo '<br/>';
$my_path = dirname(__FILE__) . '/path/' . $i;
if (!is_dir($my_path)) {
echo $i . ' dir not exist ';
if (mkdir($my_path, 0777)) {
echo $i . ' dir created ';
} else {
echo $i . ' dir not created ';
}
} else {
echo $i . ' dir already exists ';
}
}
?>