迭代返回文件夹以查找wp-config.php

迭代返回文件夹以查找wp-config.php,php,directory,constants,Php,Directory,Constants,我有一个在Wordpress范围之外执行的php脚本,但是我需要使用wp config.php中定义的Wordpress常量。我想让脚本平台独立 我的脚本位于子文件夹(一个主题)中,我正在尝试迭代返回以查找wp config。我的主题文件应该能够放置在不同的DEP(子文件夹)最好 我不能在我的while循环中得到一个单圈的陈述,但我不明白为什么!我正在尝试设置条件,使其在系统根目录中停止,如(c:) 在@Pekka的建议下,我改写了这个函数!:-) 函数getWPConfig(){ $dir=d

我有一个在Wordpress范围之外执行的php脚本,但是我需要使用
wp config.php
中定义的Wordpress常量。我想让脚本平台独立

我的脚本位于子文件夹(一个主题)中,我正在尝试迭代返回以查找wp config。我的主题文件应该能够放置在不同的DEP(子文件夹)最好

我不能在我的while循环中得到一个单圈的陈述,但我不明白为什么!我正在尝试设置条件,使其在系统根目录中停止,如(c:)


在@Pekka的建议下,我改写了这个函数!:-)

函数getWPConfig(){ $dir=dirname(文件名); $lastDir=\uuuuu文件\uuuuu; while(strlen($dir)您可以使用
dirname()
遍历目录树。比按你的方式切割容易。
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;
}
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;
}