從目錄中獲取文件我建立一個wordpress主題,我試圖從一個目錄中使用php隨機順序獲取所有的jpg文件.... 即時使用在win7(localhost)上的Xampp。從隨機順序使用php(xampp)
這是代碼:
<?
$dir = get_template_directory_uri().'/images/top/';
$file_display = array ('jpg', 'jpeg');
if(file_exists($dir) == false){
echo 'Directory \'', $dir, '\' not found';
} else {
$dir_contents = scandir($dir);
shuffle($dir_contents);
foreach ($dir_contents as $file) {
$file_type = strtolower(end(explode('.', $file)));
if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true){
echo '<img src="', $dir, '/', $file, '" alt="', $file, '" />';
}
}
}
?>
我總是得到
Directory 'http://localhost/ni/wp-content/themes/A/images/top/' not found
我也試圖改變
$dir = get_template_directory_uri().'/images/top/';
到:
$dir = "C:\xampp\htdocs\Ni\wp-content\themes\A\images\top\";
但仍然沒有運氣,任何幫助將不勝感激!
你是否在'(file_exists($ dir)== false)前有'if'前面的'test? – 2013-05-02 08:55:28
是的,我編輯了這個問題,謝謝!我在我的原始代碼上擁有它! – Firezilla12 2013-05-02 08:57:17
嘗試用'get_template_directory'替換'get_template_directory_uri',因爲'get_template_directory_uri'提供了一個URI(並且你不想這麼做) – EaterOfCode 2013-05-02 08:59:54