我正在尝试使用我的代码。我基本上检查一个文件夹是否存在,并且该目录中存在任何名为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 "";
}
答案 0 :(得分:3)
看起来你应该做的事情。只需将=>
更改为简单的=
,并且不要忘记增加:
while (file_exists($folderPath.$folderSubNumber)) {
echo '...';
$folderSubNumber += 1;
}
(另外,else
部分是不被允许的。但是你不需要它,所以没有丢失。)
答案 1 :(得分:2)
一个简单的方法:
$subdirs = glob(cdnurl . "assets/" . pid . "/*", GLOB_ONLYDIR);
它将返回指定目录中的所有子目录。参考:http://php.net/glob
答案 2 :(得分:1)
请准备好一些建设性的批评。
这段代码有很多问题。首先,我会添加评论来解释一些错误,以及一些无用的事情。
$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
答案 3 :(得分:0)
// 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;
}
}