php glob得到正确的路径

问题描述:

我需要将图像从特定的文件夹加载到数组。php glob得到正确的路径

我使用glob功能,并有一些路径问题。

我通过$ jquery中的$ .getJSON调用php脚本并传递文件夹中第一个图像的url。

$filename = $_GET['fname']; 
$thumbsPath = dirname($filename);  
$images = glob($thumbsPath.'*.{jpg,jpeg,png,gif}', GLOB_BRACE); 
  • $文件名为localhost/myproject的/图像/ 10.JPG
  • $ thumbsPath为localhost/myproject的/图像/
  • php的文件夹scritp为localhost/myproject的/脚本/

Filestructure是:

 
localhost 
-scripts/ 
--script.js 
--globscript.php 
-images/ 
--10.jpg 
--11.jpg 
--12.jpg 
--... 
-index.html 

和glob函数仅当我使用../images/作为路径返回结果。

我该如何解决?谢谢!

而其他的问题,我需要网址或pathes相对的index.html所得数组中......

+0

所以,如果'$ thumbsPath = '../图像/';' 有用? – 2012-02-27 19:56:25

+0

是的,它以这种方式工作,但路径必须基于图像url自动生成... – Sobakinet 2012-02-27 20:00:48

+0

所有图像都存储在'/ images'正确吗? – 2012-02-27 20:01:38

也许这会做你想要什么:

$dir = dirname($filename); 
$dh = opendir($dir); 
while ($img = readdir($dh)) { 
    // type validation with filetype($dir.$img); 
} 
closedir($dh); 

这种方式,它不应该不管哪里是图像文件夹

+0

应该工作!谢谢! – Sobakinet 2012-02-27 23:40:41

+0

它看起来像只支持1级的子目录。假如我有多层次的子目录,例如/ img/works /,它只会将光标指向img。 – 2015-01-15 08:25:38

试试这个:

$filename = $_GET['fname']; 
$thumbsPath = $_SERVER['DOCUMENT_ROOT'] . 'myproject/images/' . $filename;  
$images = glob($thumbsPath.'*.{jpg,jpeg,png,gif}', GLOB_BRACE); 
+0

不,它不能与C:/ xampp/htdocs/localhost/myproject/images/ – Sobakinet 2012-02-27 20:16:43

+0

我编辑了'$ thumbPath'变量。试试看。基本上,你需要定义图像的路径。 '$ _SERVER ['DOCUMENT_ROOT']'会带你到根,然后从那里建立路径。 – 2012-02-27 21:02:59