添加图像从一个文件夹与php,需要正确的正则表达式函数来使用


adding images from a folder with php, need correct regex function to use

我正在实现一个脚本,将图像从文件夹添加到页面。我得到的错误如下:

警告:sort()期望参数1为array, null在C:'xampp'htdocs'mysite'design.php第25行

警告:第26行C:'xampp'htdocs'mysite'design.php中为foreach()提供的参数无效

原始脚本使用ereg,然后我用preg_match替换了它,但是仍然存在问题,总有一天我会了解所有这些函数实际上返回的是什么。

提前感谢。我感觉有复制品,但我还没有找到。

$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>";
}
$dir     = "../mysite/images/";
$gallery = glob($dir."/*.png");
sort($gallery);

preg_match缺少分隔符:

preg_match("/'.png$/",$filename))
            ^      ^

你还需要在while循环之前添加这个:

$gallery = array();