2013-04-22 60 views
0

我有一个php脚本,它在Wordpress的范围之外执行,但是我需要使用一个在wp-config.php中定义的Wordpress常量。我想让剧本平台变得坚强。迭代回文件夹找到wp-config.php

我的脚本是在子文件夹(一个主题),我试图迭代找到wp-config。我的主题文件应该可以放在不同的文件夹(子文件夹)中!

虽然我无法在while循环中获得声明来运行一个单圈,但我不明白为什么!我想设定的条件,这样它会在系统根目录停止像(C :)

function getWPConfig() { 
    $dir = dirname(__FILE__); 
    $dir = addslashes($dir);  
    while ($pos = strpos($dir, "\\") !== false) { //&& strpos($dir, (string)DIRECTORY_SEPARATOR) > 1 
     $wpConfig = $dir . DIRECTORY_SEPARATOR .'wp-config.php'; 
     if (file_exists($wpConfig)) { 
      echo 'Found wp-config: '. $wpConfig; 
      return $wpConfig; 
     } else { 
      'Could not find: '. $dir . DIRECTORY_SEPARATOR .'wp-config.php'; 
     } 
     $tempDir = explode(DIRECTORY_SEPARATOR, $dir); 
     $end = end($tempDir); 
     $dir = str_replace($end, '', $tempDir); 
    } 
    return; 
} 
+1

你可以使用'目录名()'向上遍历目录树。比切割你的方式更容易。 – 2013-04-22 08:09:12

回答

1

后@佩卡的建议,而不是我写了这个功能! :-)

function getWPConfig() { 
    $dir = dirname(__FILE__); 
    $lastDir = __FILE__; 
    while (strlen($dir) < strlen($lastDir)) { 
     $wpConfig = $dir . DIRECTORY_SEPARATOR .'wp-config.php'; 
     if (file_exists($wpConfig)) { 
      return $wpConfig; 
     } 
     $lastDir = $dir; 
     $dir = dirname($dir); 
    } 
    return; 
}