Reputation: 1407
I'm implementing a script that adds images to a page from a folder. The errors i'm getting are as follows:
Warning: sort() expects parameter 1 to be array, null given in C:\xampp\htdocs\mysite\design.php on line 25
and
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\mysite\design.php on line 26
the original script used ereg, which i then replaced with preg_match, but there are still problems, someday i will learn what all of these functions are actually returning.
thanks in advance. i have the feeling there is duplicate, but i haven't found it yet.
$dir = "../mysite/images/";
$dh = opendir($dir);
while($filename = readdir($dh))
{
$filepath = $dir.$filename;
//pregmatch used to be ereg. which function is best?
if (is_file($filepath) and preg_match("\.png$",$filename))
{
$gallery[] = $filepath;
}
}
sort($gallery);
foreach($gallery as $image)
{
echo "<hr>";
echo"<img src='$image'><br>";
}
Upvotes: 1
Views: 101
Reputation: 157850
$dir = "../mysite/images/";
$gallery = glob($dir."/*.png");
sort($gallery);
Upvotes: 4
Reputation: 454990
Your preg_match is missing delimiters:
preg_match("/\.png$/",$filename))
^ ^
Also you need to add this before your while loop:
$gallery = array();
Upvotes: 2