2011-11-26 21 views
1

我正在实现一个脚本,它将图像添加到文件夹中的页面。是,我发现了错误如下:使用php从文件夹添加图像,需要使用正确的正则表达式函数

警告:排序()预计参数1是阵列,在C空给出:\ XAMPP \ htdocs中\ mysite的\上线25

design.php

警告:在C的foreach()提供参数无效:\ XAMPP \ htdocs中\ mysite的上线路26

\ design.php使用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>"; 
} 

回答

2

您的preg_match缺少分隔符:

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

而且你需要在while循环之前,补充一点:

$gallery = array(); 
4
$dir  = "../mysite/images/"; 
$gallery = glob($dir."/*.png"); 
sort($gallery); 
+0

+1,为什么推倒重来。 – codaddict

+0

不错的一个。我从来没有遇到过这个功能。好好学习它存在。 –

+0

这有什么不同? – expiredninja