我需要将图像从特定文件夹加载到数组。
我使用 glob 函数,但路径有一些问题。
我通过 $.getJSON 从 jquery 调用 php 脚本,并传递文件夹中第一个图像的 url。
$filename = $_GET['fname'];
$thumbsPath = dirname($filename);
$images = glob($thumbsPath.'*.{jpg,jpeg,png,gif}', GLOB_BRACE);
- $文件名是 localhost/myproject/images/10.jpg
- $thumbsPath 是 localhost/myproject/images/
- php scritp 文件夹是 localhost/myproject/scripts/
文件结构是:
localhost -scripts/ --script.js --globscript.php -images/ --10.jpg --11.jpg --12.jpg --... -index.html
只有当我使用 ../images/ 作为路径时,glob 函数才会返回结果。
如何解决?谢谢!
另一个问题是,我需要在结果数组中相对于 index.html 的 url 或路径...
最佳答案
也许这会做你想要的:
$dir = dirname($filename);
$dh = opendir($dir);
while ($img = readdir($dh)) {
// type validation with filetype($dir.$img);
}
closedir($dh);
这样,图像文件夹在哪里就不重要了
关于php glob 获取正确的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9471295/