简体   繁体   English

IIS 上的 PHP __FILE__ 为使用 MKLINK 创建的符号链接文件夹返回错误的路径

[英]PHP __FILE__ on IIS returns wrong path for symlinked folders created with MKLINK

I am debugging a problem that is unique to Microsoft IIS, specifically IIS10, but I don't know whether other versions of IIS also have this problem.我在调试一个微软IIS特有的问题,特别是IIS10,不知道其他版本的IIS是否也有这个问题。 I have a problem with a WordPress plugin, that works correctly when I run it on an Apache server, or in my Local by Flywheel test environment on any of the Apache/Linux/nginx web servers.我有一个 WordPress 插件的问题,当我在 Apache 服务器上运行它时,或者在任何 Apache/Linux/nginx Web 服务器上的 Local by Flywheel 测试环境中运行它时,它可以正常工作。

The problem involves the PHP built-in constant __FILE__ to a symbolically linked folder.问题涉及到符号链接文件夹的 PHP 内置常量 __FILE__。

I created symbolic link to my plugin development folder as the following:我创建了到我的插件开发文件夹的符号链接,如下所示:

Website is here:        c:\inetpub\my-web-site
The plugin lives here:  c:\users\me\onedrive\plugins\my-plugin

I created a symbolic link in the folder:我在文件夹中创建了一个符号链接:

c:\inetpub\my-web-site\wp-content\plugins

using the following commands on the command line.在命令行上使用以下命令。

c:
cd \inetpub\my-web-site\wp-content\plugins
mklink /J my-plugin c:\users\me\onedrive\plugins\my-plugin

The web server sees the plugin folder as: Web 服务器将插件文件夹视为:

c:\inetpub\my-web-site\wp-content\plugins\my-plugin

That's the simple part.这就是简单的部分。

In PHP, from inside a WordPress website, the PHP constant __FILE__ (IIS only) returns the wrong path.在 PHP 中,从 WordPress 网站内部,PHP 常量 __FILE__(仅限 IIS)返回错误的路径。 Specifically, calling PHP constant __FILE__ returns the wrong path.具体来说,调用 PHP 常量 __FILE__ 会返回错误的路径。

Add this code:添加此代码:

$path = __FILE__;

To this file:到这个文件:

c:\inetpub\my-web-site\wp-content\plugins\my-plugin\my-plugin.php

Both $path will return: $path 都将返回:

c:\users\me\onedrive\plugins\my-plugin\my-plugin.php

I expected __FILE__ to return:我希望 __FILE__ 返回:

c:\inetpub\my-web-site\wp-content\plugins\my-plugin\my-plugin.php

In other words, on IIS10, the PHP constant __FILE__ returns the physical (target) path to the symbolically linked folder as seen by the operating system, not the physical path to the files as seen by the web server.换句话说,在 IIS10 上,PHP 常量 __FILE__ 返回操作系统看到的符号链接文件夹的物理(目标)路径,而不是 Web 服务器看到的文件的物理路径。

As it stands, this completely defeats the purpose of using symbolically linked folders in IIS.就目前而言,这完全违背了在 IIS 中使用符号链接文件夹的目的。

My question: Does anybody know 1) Is there a remedy to this?我的问题:有人知道 1) 有没有办法解决这个问题? 2) is there a work around which does not involve the use of WP_CONTENT_DIR? 2)是否有不涉及使用 WP_CONTENT_DIR 的解决方法? and 3) Am I missing something? 3)我错过了什么吗?

This is expected behavior from PHP and is documented.这是 PHP 的预期行为,并记录在案。 Magic constants for paths/filenames always resolve the path so that symlinks are replaced with their target paths.路径/文件名的魔术常量始终解析路径,以便符号链接替换为其目标路径。

The suggestion in that SO question is to use $_SERVER["SCRIPT_FILENAME"] . SO 问题中的建议是使用$_SERVER["SCRIPT_FILENAME"] Though that may not not work if the file you are testing has been included via another PHP file in another directory.尽管如果您正在测试的文件已通过另一个目录中的另一个 PHP 文件包含在内,这可能不起作用。 There are a couple other suggestions but ultimately the solution may come down to mounting the directory to a path (like you do a drive).还有一些其他建议,但最终解决方案可能归结为将目录安装到路径(就像你做驱动器一样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM