您如何使PHP,Symlinks和__FILE__一起良好地工作? [英] How do you get PHP, Symlinks and __FILE__ to work together nicely?

查看:90
本文介绍了您如何使PHP,Symlinks和__FILE__一起良好地工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在localhost上.我具有以下目录结构:

On localhost. I have the following directory structure:

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

/share/www/portfolio/wp-content/symlink

symlink/trunk/.../plugins/的符号链接.基本上,这是因为我需要测试并安装多个WordPress,但是我不想移动插件并将其复制并粘贴到任何地方.

Where symlink is a symbolic link to /trunk/.../plugins/. Basically, this is because I need to test multiple WordPress installs and set them up, but I don't want to have to move plugins around and copy and paste them everywhere.

但是,有时我需要爬上目录树以包含配置文件:

However, sometimes I need to crawl up the directory tree to include a config file:

 $root = dirname(dirname(dirname(dirname(__FILE__))));
      if (file_exists($root.'/wp-load.php')) {
          // WP 2.6
          require_once($root.'/wp-load.php');
      }

该文件夹始终解析为:

/share/www/trunk

即使正在执行插件并将其包含在其中

Even when the plugin is being executed and included in

/share/www/portfolio/.

在PHP中是否可以通过在符号链接中执行的脚本在/share/www/trunk/.../plugins目录中将文件包含在share/www/portfolio目录中?

Is it possible in PHP to include files in the share/www/portfolio directory from a script executing in a symlink to the /share/www/trunk/.../plugins directory?

虽然此问题仅在我的测试服务器上发生,但我想拥有一个可安全分发的解决方案,因此无法选择额外的级别.

While this problem only happens on my test server, I'd like to have a safely distributable solution so crawling up an extra level is not an option.

推荐答案

我在您的代码中看到的问题是__FILE__自动解决符号链接.

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

摘自PHP手册,网址为魔术常数

From the PHP Manual on Magic Constants

...自PHP 4.0.2起,__FILE__始终包含已解析符号链接的绝对路径...

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

您可以尝试改用$_SERVER["SCRIPT_FILENAME"].

$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');
  }

请注意,我已将realpath()函数添加到了根目录.根据您的设置,您可能会需要也可能不需要.

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

使用$_SERVER["SCRIPT_FILENAME"]代替$_SERVER["PHP_SELF"]作为文件系统路径.

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

这篇关于您如何使PHP,Symlinks和__FILE__一起良好地工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆