2013-06-19 25 views
0

我有一些代碼:PHP不查找文件

<div id="image-cycle-container"> 
<ul id="image-cycle"> 
<?php 
    //Select the folder which the images are contained within and store as an array 
    $imgDir = get_stylesheet_directory_uri() . '/img/image-cycle/'; 
    $images = glob($imgDir . '*.jpg'); 
    foreach($images as $image){ 
     echo 'something'; 
     echo "<li><img src='".$image."' /></li>\n"; 
    }  
?> 
</ul> 

,問題是,沒有圖像顯示(雖然他們絕對不存在)。我可以絕對引用它們,但PHP沒有找到任何東西/數組是空的。我使用WAMP開發網站,我開始懷疑這是否是我生活中的禍根......

+0

'$ image'的值是什麼? –

+0

get_stylesheet_directory_uri()的輸出是什麼? – KyleK

+0

@newfurniturey $ imgDir的值是「http://127.0.0.1/xxxx/wp-content/themes/responsive-child/img/image-cycle/」,其中xxxx是網站所在的文件夾。這是一個問題?似乎我不能鏈接沒有它鏈接... – Titus

回答

0

通過評論,從get_stylesheet_directory_uri()方法返回的路徑是http://127.0.0.1/xxxx/wp-content/themes/responsive-child/

此路徑直接用於PHP glob()函數。

簡短的回答直接來自於文檔:

注:此功能將無法在remote files工作,被檢查必須通過服務器的文件系統訪問。

一個可能的解決方案,這一點,因爲你知道你的當前域是什麼,將剝離域名從路徑從get_stylesheet_directory_uri()返回,並在全路使用結果:

$domain = 'http://127.0.0.1/'; 

$imgDir = get_stylesheet_directory_uri() . '/img/image-cycle/'; 
$imgDir = substr($imgDir, strlen($domain)); // strip the domain 

$images = glob($imgDir . '*.jpg'); 

這將帶回一組圖像,您可以像當前正在做的那樣迭代。但是,此列表將與腳本正在執行的當前目錄相關,因爲它們不會以/或域名作爲前綴。因此,我們可以將其添加回foreach循環中:

foreach($images as $image) { 
    $image = $domain . $image; 
    // ... 
}