How do you get PHP, Symlinks and __FILE__ to work together nicely?

前端 未结 6 1692
既然无缘
既然无缘 2020-12-02 15:19

On localhost. I have the following directory structure:

/share/www/trunk/wp-content/plugins/otherfolders

/share/www/portfolio/wp-content/s

6条回答
  •  执念已碎
    2020-12-02 16:15

    The problem that I see with your code is that __FILE__ resolves symlinks automatically.

    From the PHP Manual on Magic Constants

    ... Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved ...

    You can try using $_SERVER["SCRIPT_FILENAME"] instead.

    $root = realpath(dirname(dirname(dirname(dirname($_SERVER["SCRIPT_FILENAME"])))));
      if (file_exists($root.'/wp-load.php')) {
          // WP 2.6
          require_once($root.'/wp-load.php');
      }
    

    Note that I added the realpath() function to the root directory. Depending on your setup, you may or may not need it.

    EDIT: Use $_SERVER["SCRIPT_FILENAME"] instead of $_SERVER["PHP_SELF"] for the file system path.

提交回复
热议问题