Reputation: 395
I have a mistery with my PHP code going wrong in some cases and I don't understand why. My current code is displaying all photos related to the client by a range.
Example: pick all inside the folder from pictures 300 to 310 and display them.
Example of photos names inside my folder: C20_0385, C20_0386, C20_0389, C20_0400...
Current PHP code: with photos from 385 to 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\">";
}
That works perfectly and now if I do $start = 385; $end = 450;
It write errors:
Warning: glob(): Pattern exceeds the maximum allowed length of 260 characters
Warning: Invalid argument supplied for foreach()
If I do from $start = 385; $end = 435;
it shows the whole folder currently up to C20_503.JPG
It looks like for small range more or less 40 photos it does the job but for more something is going wrong.
Upvotes: 0
Views: 819
Reputation: 9093
The error states that the glob pattern cannot be longer than 260 characters; you're going to have to use an alternative approach:
$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);
Upvotes: 3
Reputation: 94682
This function range($start, $end)
is generating a set of numbers.
When you $start = 385; and $end = 420;
the list contains 35 entries
When you $start = 385; and $end = 435;
the list contains 50 entries
It is this that is making the Pattern exceeds the maximum allowed length of 260 characters
error
You will have to limit the numbers so you dont cause the generated string to exceed the allowed length.
Upvotes: 2