我有一个谜与我的PHP代码出错在某些情况下,我不明白为什么。我当前的代码是按范围显示与客户端相关的所有照片。
示例:从图片300到310中选择文件夹内的所有图片并显示它们。
文件夹中的照片名称示例:C20_0385, C20_0386, C20_0389, C20_0400…
当前PHP代码:照片从385到420.
$start = 385:
$end = 420;
$dirname = "smallphotos/$jour_creation - $date_creation/$session/$dossier";
$filenames = glob("$dirname/*{" . implode(",", range($start, $end)) . "}*", GLOB_BRACE);
foreach ($filenames as $filename)
{
echo "<img class='"img-responsive'" src='"$filename'" alt='"$filename'">";
}
效果很好现在如果我输入$start = 385; $end = 450;
它写错误:
Warning: glob(): Pattern exceeds the maximum allowed length of 260 characters
Warning: Invalid argument supplied for foreach()
如果我选择从$start = 385; $end = 435;
它会显示当前整个文件夹直到C20_503.JPG
对于小范围的大约40张照片,它看起来可以完成工作,但对于更多的照片,它就出错了。
错误说明glob模式不能超过260个字符;您将不得不使用另一种方法:
$filenames = array();
$dir = opendir( $dirname );
while (false !== ($file = readdir($dir)))
{
if ( is_file( $file ) && preg_match( "/^.*?_('d+)/", $file, $m )
&& $m[1] >= $start && $m[1] <= $end
)
$filenames[] = "$dirname/$file";
}
closedir($handle);
这个函数range($start, $end)
正在生成一组数字。
当您$start = 385; and $end = 420;
时,列表包含35个条目
当您执行$start = 385; and $end = 435;
时,列表包含50个条目
这是造成Pattern exceeds the maximum allowed length of 260 characters
错误
你必须限制数字,这样你就不会使生成的字符串超过允许的长度