Php 涉及虚拟文件夹时,服务器变量document root无法很好地处理文件\u

Php 涉及虚拟文件夹时,服务器变量document root无法很好地处理文件\u,php,file-exists,document-root,server-variables,virtual-path,Php,File Exists,Document Root,Server Variables,Virtual Path,我在IIS上有一个虚拟文件夹。 当我创建服务器文档根目录时,我不会获取文件\u exists查找文件。但有趣的是,如果我使用include(或要求相关指令),则会找到相同的文件 范例 $full_path = $_server['DOCUMENT_ROOT'] . "/file.txt"; include($full_path); // works fine. if file_exists($full_path) : // returns false! 同样,这只是当我有一个虚拟文件夹涉及

我在IIS上有一个虚拟文件夹。 当我创建服务器文档根目录时,我不会获取
文件\u exists
查找文件。但有趣的是,如果我使用include(或要求相关指令),则会找到相同的文件

范例

$full_path = $_server['DOCUMENT_ROOT'] . "/file.txt";
include($full_path); // works fine. 
if file_exists($full_path) : // returns false!
同样,这只是当我有一个虚拟文件夹涉及

我想我必须使用不同的服务器变量,这不受是否存在虚拟文件夹的影响

最后,我希望下面的方法能够奏效

/wwwroot/file.txt
应与此


file\u存在($\u server['?']。“/file.txt”)

我不知道IIS虚拟文件夹的行为,但您可以从全局常量
\uu file\uuu
获取当前脚本的完整文件路径,请参阅

如果您知道脚本中的相对路径,请使用以下方法:

$filepath = dirname(__FILE__) . "/../../file.txt";

但不确定这是否解决了您的问题。

我不知道IIS虚拟文件夹的行为,但您可以从全局常量
\uuuuuuu file\uuuuu
获取当前脚本的完整文件路径,请参阅

如果您知道脚本中的相对路径,请使用以下方法:

$filepath = dirname(__FILE__) . "/../../file.txt";

但不确定这是否解决了您的问题。

使用配置文件并定义baseroot和publicroot

<?php
// myconfig.php -- stored in the public root
$direx = explode("/", getcwd());
DEFINE ('BASEROOT', '/'.$direx[1].'/'.$direx[2].'/'); // host/mysite/
DEFINE ('PUBLICROOT', '/'.$direx[1].'/'.$direx[2].'/'.$direx[3].'/'); // host/mysite/public_html/
?>

<?php
require_once('myconfig.php'); // called on every page

IF (File_Exists(PUBLICROOT.'some_folder/file.txt')) { /* do something */ }

include_once(PUBLICROOT.'some_folder/file.txt');
?>

使用配置文件并定义baseroot和publicroot

<?php
// myconfig.php -- stored in the public root
$direx = explode("/", getcwd());
DEFINE ('BASEROOT', '/'.$direx[1].'/'.$direx[2].'/'); // host/mysite/
DEFINE ('PUBLICROOT', '/'.$direx[1].'/'.$direx[2].'/'.$direx[3].'/'); // host/mysite/public_html/
?>

<?php
require_once('myconfig.php'); // called on every page

IF (File_Exists(PUBLICROOT.'some_folder/file.txt')) { /* do something */ }

include_once(PUBLICROOT.'some_folder/file.txt');
?>


谢谢,但我的问题不是试图找出我所在文件的完整路径。我应该能够传递一个虚拟根目录(我在浏览器上看到的方式),PHP应该为我提供浏览器上显示的虚拟路径的真实文件路径。示例:site.com/folderX/fileX.txt应该可以通过doc_root找到。“/folderX/fileX.txt”谢谢,但我的问题不是试图找出我所在文件的完整路径。我应该能够传递一个虚拟根目录(我在浏览器上看到的方式),PHP应该为我提供浏览器上显示的虚拟路径的真实文件路径。示例:site.com/folderX/fileX.txt应该可以通过doc_root找到。“/folderX/fileX.txt”