为什么is_readable&;是可写的&;file_exists对于URL路径而不是同一目录中的文件失败


Why would is_readable & is_writable & file_exists fail for URL path and not for a file in the same directory?

我已经在谷歌上搜索这个问题两天了,真的没有找到能给我一个可靠答案的人。也许stackoverflow的天才们能帮上忙?

我的难题是:我试图使用指向服务器上文件的URL语法来读写文件。(请参阅下面的代码)只要文件名与php文件在同一目录中,并且我没有在上面使用任何URL套接字,代码就可以正常工作。http://127.0.0.1/site_folder/text.xml)我在所有三个测试中都失败了(is_readable、is_writeable、file_exists)。为什么会这样?有没有办法纠正它,这样我就可以读写这个文件了?

我的安装程序:Microsoft Vista计算机上的Microsoft IIS 7.0.6000.16386上的PHP 5.3.2PHP目录:c:''Program Files(x86)''PHP''安全性:我已经为IUSR帐户设置了文件目录和文件本身的读取、写入、执行、列出目录的权限。这个php文件目前与test.xml文件位于同一目录中。但我想让这个URL正常工作,这样我将来就可以调整不同目录中的文件。

php.ini配置:我已经尝试过这些设置和其他设置:

open_basedir=Off; 
fastcgi.impersonate = 1;
display_errors = On;
error_reporting = E_ALL & ~E_STRICT;
safe_mode = Off;
allow_url_fopen = Off; (and tried On, neither works);
allow_url_include = Off; (and tried On, neither works);

这是我的简单代码示例:

<?php
        $filename = "http://127.0.0.1/sitename/admin/interface/test.xml"; // this one fails
//      $filename = "text.xml" // this one works fine
    echo $filename;
        if (is_readable($filename)) {
                echo "<br />The file is readable...<br />";
        } else {
                echo "<br />The file is not readable...<br />";
        }
        if (is_writable($filename)) {
                echo "The file is writable...<br />";
        } else {
                echo "The file is not writable...<br />";
        }
        if (file_exists($filename)) { 
                echo "File exists...<br />";
        } else {
                echo "File cannot be found...<br />";
        }
?>

我得到的输出是:

http://127.0.0.1/sitename/admin/interface/test.xml
The file is not readable...
The file is not writable...
File cannot be found...

知道为什么会发生这种事吗?顺便说一句,同样的事情也发生在我的Macbook Pro上的PHP 5.3.2上,上面有Apache Server。

http://protocol包装器不支持stat()函数族(请参阅:http://php.net/manual/wrappers.http.php查看支持什么)。stat()支持对于file_exists、is_writeable和is_readable是必要的。

文件://协议包装器(默认的,在您的工作示例中使用)确实支持stat():http://php.net/manual/wrappers.file.php

您可以在此处阅读有关协议和包装器的更多信息:http://php.net/manual/wrappers.php

$filename = "//127.0.0.1/sitename/admin/interface/test.xml";

您可以尝试像../../test.xml一样更改此路径,或者在同一目录中使用test.xml我试过了,同样的问题也解决了。

如果要执行文件I/O,则传入实际路径(使用$_SERVER['DOCUMENT_ROOT']可能有助于指定这些路径)。PHP的fopen支持对URL的特殊处理,但其他文件I/O函数的包装器不支持。