如果文件夹存在,对每个子文件夹执行循环


PHP If folder exists do a loop for each subfolder

我正试图让我的代码工作。我基本上检查如果一个文件夹存在,并且该目录下的子文件夹存在3或更多(3,4,5)然后执行一个循环,否则不执行任何操作。

// the folder path
$folderPath="".cdnurl."assets/".pid."";
// looks like site.com/assets/342/
// is there a sub folder called 3 or more, 3,4,5 and loop from 3
// site.com/assets/342/3
// site.com/assets/342/4
// etc, we only loop if 3 exists other wise nothing
$folderSubNumber =>3;
    while(file_exists($folderPath.$folderSubNumber)) {
        echo '<li><img src="assets/pid/'.folderSubNumber.'';
    } else {
    // nothing 
        echo "";    
    }

看起来你应该这样做。只需将=>更改为简单的=,并且不要忘记增加:

while (file_exists($folderPath.$folderSubNumber)) {
    echo '...';
    $folderSubNumber += 1;
}

(另外,else部分是不允许的。但是这里不需要,所以没有什么损失。

一个简单方法:

$subdirs = glob(cdnurl . "assets/" . pid . "/*", GLOB_ONLYDIR);

返回指定目录中的所有子目录。裁判:http://php.net/glob

请准备好接受一些建设性的批评。

这段代码有很多错误。首先,我将添加注释来解释一些错误以及一些没有意义的事情。


$folderPath="".cdnurl."assets/".pid."";
// 1. Single-quotes will perform slightly better.
// 2. There is no need for the first "". or the final ."" - they do nothing.
// 3. Ideal: $folderPath = cdnurl.'assets/'.pid;
// 4. This assumes that cdnurl and pid are constants declared with the define() command. If they are not constants, you need dollar-signs, which would make it:
//    $folderPath = $cdnurl.'assets/'.$pid;
$folderSubNumber => 3;
// You cannot put a "more than X" or "less than X" in a variable. The => is used in foreach() loops for a completely different purpose, and when declaring values in an array only when the array is originally declared. (In other words; in this case, this does nothing.)
// Indentation really does matter. This should be indented the same as the code above.
while(file_exists($folderPath.$folderSubNumber)) {
    // 1. $folderSubNumber never changes and so this while-loop always asks the exact same question.
    // 2. You don't have a directory separator "/", so this will append $folderSubNumber straight to pid, above.
    echo '<li><img src="assets/pid/'.folderSubNumber.'';
    // 1. folderSubNumber needs a dollar-sign because it's a variable. If it is not defined as a constant, it will simply be the literal string "folderSubNumber".
    // 2. The appended .'' does nothing and shouldn't be there.
    // 3. You are neither closing the <img> tag, nor the <li> tag, nor in fact the src-attribute.
    // 4. Ideal: echo '<li><img src="assets/pid/'.$folderSubNumber.'" /></li>';
} else {
    // 1. There is no "else" in while.
    // 2. You don't need an "else" if the intention is to do nothing.
    echo "";
    // This is 100% pointless, it does nothing.
}

然后需要的是在while循环中尝试后增加$foldeSubNumber(参见'sdleihssirhc'的答案)。但也要注意,您可能需要在$folderPath和$folderSubNumber之间使用目录分隔符。即:$folderPath.'/'.$folderSubNumber

祝你好运!

// Build folder path
$folder_path = cdnurl . 'assets/' . $pid . '/';
// Loop from 3 to 5
for ($i = 3; i <= 5; $i++) {
  // Checking for a valid sub-directory
  // Will check sub-directory e.g.: site.com/assets/342/3/
  if ( is_dir($folder_path . $i . '/') ) {
    // Sub-directory exists
    echo $folder_path . $i;
  } else {
    // No Sub-directory, break out of for loop
    // This is will stop PHP for looking for sub-directory 4 or 5
    break;
  }
}