php glob 获取正确的路径

标签 php path glob

我需要将图像从特定文件夹加载到数组。

我使用 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/

相关文章:

php - 如何简化 PHP 中的检查/验证逻辑?

php - 我有两个 if 和一个 else 条件,但 else 条件不起作用并且总是显示

php - Symfony 友好的购物车

php - 使用 PHP ftp_get() 检索具有通配 rune 件名的文件

java - 如何为 Dataflow 作业指定多个输入路径

javascript - 使用键盘按键导航网页(在内容脚本内)

Angular 6/在 tsconfig.lib.json 中声明库的路径

xml - 从 CLOB 中的 XML,到带有路径列表的 Oracle 表

c# - 在哪里可以看到 C# 中预定义方法的代码?

python - 从特定目录中选择文件