【发布时间】:2021-02-05 04:31:30
【问题描述】:
我有这个功能应该指向正确的图像文件夹(图像)并显示其图像,但它显示根路径中的所有图像和(图像)文件夹中的图像。我必须进行哪些更改才能使其仅显示属于 (images) 文件夹的图像?。
function searchSite( $path = 'images', $level = 0 ) {
$skip = array( 'cgi-bin', '.', '..' );
$look_for = array( '.jpg', '.gif', '.png', '.jpeg' );
$dh = @opendir( $path );
while ( false !== ( $file = readdir( $dh ) ) ) {
$file_ext = substr( $file, -4, 4 );
if ( !in_array( $file, $skip ) ) {
if ( is_dir( "$path/$file" ) ) {
searchSite( "$path/$file", ( $level + 1 ) );
} else if ( in_array( $file_ext, $look_for ) ) {
echo "<option value='$path/$file' />$file</option>";
}
}
closedir( $dh );
}
【问题讨论】:
-
您能否编辑您的问题以使代码在语法上正确?如果您正确缩进它也将有助于提高可读性。
-
@KIKOSoftware 我已尽力使其整洁,我只是从 DreamWeaver IDE 中复制了它。这是它的编码格式!
-
这可能发生,但更重要的问题是
{和}不平衡。 -
它仍然不平衡,并且包含其他更微妙的错误。例如:字符串
'.jpeg'是 5 个字符长,而不是 4 个字符。不过,这段代码似乎是实现递归的正确方法,理论上应该可以工作。你检查过 PHP 错误信息吗?