2009-12-23 81 views
2

我運行PHP版本5.2.11致命錯誤:未捕獲的異常 '的RuntimeException'

當我運行下面的功能:

function get_dirs($path = '.') { 
    $dirs = array(); 
    foreach (new DirectoryIterator($path) as $file) { // This is line 462. 
     if ($file->isDir() && !$file->isDot()) { 
      $dirs[] = $file->getFilename(); 
     } 
    } 
    return $dirs; 
} 

我得到這個錯誤:

Fatal error: Uncaught exception 'RuntimeException' with message 'DirectoryIterator::__construct(/home/test/test.com/wp-content/themes/mytheme/images) [<a href='directoryiterator.--construct'>directoryiterator.--construct</a>]: failed to open dir: No such file or directory' in /home/test/test.com/testing123/wp-content/themes/mytheme/functions.php:462 Stack trace: #0 /home/test/test.com/testing123/wp-content/themes/mytheme/functions.php(462): DirectoryIterator->__construct('/home/test/wie...') #1 /home/test/test.com/testing123/wp-content/themes/mytheme/functions.php(31): get_dirs('/home/test/wie...') #2 /home/test/test.com/testing123/wp-settings.php(717): include('/home/test/wie...') #3 /home/test/test.com/testing123/wp-config.php(76): require_once('/home/test/wie...') #4 /home/test/test.com/testing123/wp-load.php(30): require_once('/home/test/wie...') #5 /home/test/test.com/testing123 in /home/test/test.com/testing123/wp-content/themes/mytheme/functions.php on line 462 

更新:我發現這裏的問題是主題安裝在主URL的虛擬目錄下。我的腳本期望主題安裝在主根網址之外。

例如,在這種情況下,主題是安裝在:http://www.example.com/testing123/wp-content/themes/mytheme

不過,我期待這樣的:http://www.example.com/wp-content/themes/mytheme

和SO ...因爲它

我的路徑功能失效不考慮它可以安裝在虛擬目錄下。

我怎麼能在我的這個功能的餵養佔這個場景?

$mydir = get_dirs("$_SERVER[DOCUMENT_ROOT]/wp-content/themes/mytheme/images"); 

function get_dirs ($path = '.') { 
    $dirs = array(); 
    foreach (new DirectoryIterator($path) as $file) { 
     if ($file->isDir() && !$file->isDot()) { 
      $dirs[] = $file->getFilename(); 
     } 
    } 

    return $dirs; 
} 
+0

如果它說,它未能打開你的圖片目錄,你爲什麼認爲錯誤在編碼中? – 2009-12-23 17:34:37

+0

呃......它看起來像無法找到目錄,它沒有實例化DirectoryIterator的問題。 – mozillalives 2009-12-23 17:35:37

回答

4

把它放在try-catch塊中?

function get_dirs($path = '.') { 
    $dirs = array(); 
    try{ 
    foreach (new DirectoryIterator($path) as $file) { //this is line 462 
    if ($file->isDir() && !$file->isDot()) { 
     $dirs[] = $file->getFilename(); 
    } 
    } 
} catch(Exception $e) { 
    //log exception or process silently 
    //just for test 
    echo $e; 
} 

return $dirs; 
+0

感謝Maurice,我相信這也許是最好的選擇。我已經更新了我的問題,因爲我發現了有關導致錯誤的更多信息。它實際上是由於虛擬目錄下的意外位置。 我的代碼... $ mydir = get_dirs($ _ SERVER ['DOCUMENT_ROOT']。'/ wp-content/themes/mytheme/images'); 對「testing123」子目錄沒有帳號。 – 2009-12-23 17:47:11

2

類是存在的,但顯然你的圖片目錄不是:

/home/test/test.com/wp-content/themes/mytheme/images 

消息之中的關鍵部分:

failed to open dir: No such file or directory 

或者,如果它的存在,PHP沒有權限讀取它。

0

它看起來像第三行是告訴你該目錄無法找到:

failed to open dir: No such file or directory

我會從腳本檢查你的路徑被稱爲並調整適當

相關問題