2011-04-26 99 views
0

嘿傢伙, 我在這裏誤解了什麼?php directoryIterator問題 - 錯誤的路徑?

$dir = get_bloginfo('template_url').'/images/headers/'; 
echo $dir; 
//ouput: myblog.com/wp-content/themes/mytheme/images/headers 



$dir = new DirectoryIterator(get_bloginfo('template_url').'/images/headers/'); 
echo $dir; 
//output: nothing at all! blank page! 

控制檯拿出一個是fatal_error:

[26-Apr-2011] PHP Fatal error: Uncaught exception 'RuntimeException' with message 'DirectoryIterator::__construct(http://myblog.com/wp-content/themes/mytheme/images/headers/) [directoryiterator.--construct]: failed to open dir: not implemented' in /Users/myname/htdocs/myblog.com/wp-content/themes/mytheme/inc/header-image.php:3 Stack trace:

0 /Users/myname/htdocs/myblog.com/wp-content/themes/mytheme/inc/header-image.php(3):

DirectoryIterator->__construct('http://oberperf...')

1 /Users/myname/htdocs/myblog.com/wp-content/themes/mytheme/header.php(69):

include('/Users/myname...')

2 /Users/myname/htdocs/myblog.com/wp-includes/theme.php(1112):

require_once('/Users/myname...')

3 /Users/myname/htdocs/myblog.com/wp-includes/theme.php(1088):

load_template('/Users/myname...', true)

4 /Users/myname/htdocs/myblog.com/wp-includes/general-template.php(34):

locate_template(Array, true)

5 /Users/myname in /Users/myname/htdocs/myblog.com/wp-content/themes/mytheme/inc/header-image.php

on line 3

任何想法什麼不順心嗎?

回答

0

我敢肯定,路徑必須是絕對的文檔根目錄,而不是URL。

嘗試get_template_directory()代替:

$dir = new DirectoryIterator(get_template_directory() . '/images/headers/'); 
echo $dir; 
+0

謝謝,這是有道理的。但它不起作用!如果我嘗試你的確切代碼$ dir只是一個「。」。點!沒有路徑在那裏。沒有錯誤被拋出。如果我回顯'get_template_directory()。 '/ images/headers /''它可以正常工作,但是DirectoryIterator不會因爲某種原因獲取該值! – matt 2011-04-26 16:14:27

+0

由DirectoryIterator構造函數返回的變量是DirectoryIterator對象,您將無法將其回顯到屏幕。查看DirectoryIterator文檔以獲得更多關於如何使用它的信息http://us.php.net/manual/en/class.directoryiterator.php – jordanstephens 2011-04-26 19:25:35

1

看起來你傳遞一個網址,而不是一個路徑:

DirectoryIterator->__construct('http://oberperf...') 

您應該通過目錄構造的完整本地路徑名。

+0

確定,這是真的。但是這也不起作用:'$ dir = new DirectoryIterator('/ Users/myname/htdocs/myblog.com/wp-content/themes/mytheme/images/headers /'); echo $ dir;'< - 這只是一個「。」點! – matt 2011-04-26 16:36:46

+0

DirectoryIterator返回的對象不是字符串。你需要迭代它:foreach($ dir as $ item){echo $ item-> getFilename(); } – 2011-04-26 16:55:02

+0

完美!謝謝! – matt 2011-04-26 17:06:03