显示前缀为id的所有文件


Display all file with prefix id

我有用户文件夹/folder_name,并且有带有名称前缀的文件示例此模式id_radom.example

5__1490952185fed525d92.24525311.jpg
15__4658521860030a66d0.90328377.jpg
15__6654521861778060e1.31100475.jpg
15__6654521861778060e1.31100475.jpg

我想使用php显示所有这些图像(id=15

我正在尝试:

$path = "uploads/registered_files/".$_SESSION['user_data']['username'];
$a = glob("/".$path."/".$article->article_id."__",GLOB_BRACE);
print_r($a);

但我得到了空的array()

解决方案可能如下所示:

if (false === ($handle = opendir($path))) {
    //catch error here
}
$images = array();
while (false !== ($file = readdir($handle))) {
    preg_match('/^15__.*/', $file)) and $images[] = $file;
}
closedir($handle);
foreach ($images as $image) {
    echo '<img src="',$path,DIRECTORY_SEPARATOR,$image,'"/>';
}

/".$path."/".$article->article_id."__"指向文件系统的根,而不是您网站的根。这可能就是问题所在。

尝试删除第一个/或在其前面加上指向网站根的绝对路径。