Symfony访问web目录中的文件夹映像


Symfony access folder image in web directory

我正在使用Symfony构建一个站点,但我遇到了一个小问题。我的图像存储在/web/images/中,我想知道如何使用这个php函数glob($imagesDir.'*.{jpg,jpeg,png,gif}',glob_BRACE);以获取文件列表。实际上,它返回一个空数组。

提前谢谢。这是我直接从控制器使用的代码:

$imagesDir = $this->get('kernel')->getRootDir() . '/../web/images';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$json=json_encode($images);

您的$imagesDir没有尾部斜杠,glob()没有添加它,它只是搜索名称为images*.{jpg,jpeg,png,gif}'的文件,更改为:

$imagesDir = $this->get('kernel')->getRootDir() . '/../web/images/';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$json=json_encode($images);

它应该起作用。