2012-02-27 106 views
2

我需要將圖像從特定的文件夾加載到數組。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

回答

2

也許這會做你想要什麼:

$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

1

試試這個:

$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