为什么 is_read & is_writable & file_exists 对于 URL 路径失败,而对于同一目录中的文件则失败?
我已经在谷歌上搜索这个问题两天了,确实没有找到可以给我可靠答案的人。也许 stackoverflow 上的天才可以帮忙?
我的难题:我正在尝试使用指向我服务器上文件的 URL 语法来读取和写入文件。 (参见下面的代码)只要文件名与 php 文件位于同一目录中并且我没有在其上使用任何 URL 套接字,该代码就可以正常工作。 (即 $filename = "test.xml")但是一旦我尝试添加 URL 套接字(即 http ://127.0.0.1/site_folder/text.xml)我在所有三个测试(is_read、is_writable、file_exists)上都失败了。为什么会这样呢?有没有办法纠正它,以便我可以读取和写入该文件?
我的设置:Microsoft Vista 计算机上的 Microsoft IIS 7.0.6000.16386 上的 PHP 5.3.2 PHP 目录: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 上安装了 Apache Server,PHP 5.3.2 也发生了同样的情况。
I've been googling this question for two days and really haven't found anyone who can give me a solid answer. Maybe the geniuses at stackoverflow can help?
My conundrum: I'm trying to read and write to a file using a URL syntax for a URL pointing to a file on my server. (See code below) The code works fine so long as the filename is in the same directory as the php file and I'm not using any URL socket on it. (i.e. $filename = "test.xml") But as soon as I try to add a URL socket (i.e. http://127.0.0.1/site_folder/text.xml) I get failures on all three tests (is_readable, is_writable, file_exists). Why would this be? Is there a way to correct it so that I can read and write this file?
My Setup: PHP 5.3.2 on Microsoft IIS 7.0.6000.16386 on a Microsoft Vista machine
PHP directory: c:\Program Files (x86)\php\
Security: I have set the permissions for the directory of the file and the file itself for IUSR account to read, write, execute, list directory. This php file resides in the same directory as the test.xml file for now. But I want to get this URL to work so that I can adjust a file in a different directory in the future.
php.ini config: I've tried these settings among others:
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);
Here is my simple code sample:
<?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 />";
}
?>
And the output I'm getting:
http://127.0.0.1/sitename/admin/interface/test.xml
The file is not readable...
The file is not writable...
File cannot be found...
Any idea why this is happening?
As a side note, same thing is happening with PHP 5.3.2 on my Macbook Pro with Apache Server on it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
http:// 协议包装器不支持 stat() 系列函数(请参阅:http:// /php.net/manual/wrappers.http.php 查看支持的内容)。 file_exists、is_writable 和 is_read 需要 stat() 支持。
file:// 协议包装器(默认的,在您的工作示例中使用)确实支持 stat(): http://php.net/manual/wrappers.file.php
您可以在此处阅读有关协议和包装器的更多信息:
http://php.net/manual/wrappers.php
The http:// protocol wrapper does not support the stat() family of functions (see: http://php.net/manual/wrappers.http.php to see what is supported). stat() support is necessary for file_exists, is_writable, and is_readable.
The file:// protocol wrapper (the default one, used in your working example) does support stat(): http://php.net/manual/wrappers.file.php
You can read more about protocols and wrappers here:
http://php.net/manual/wrappers.php
您可以尝试更改此路径,如
../../test.xml
或在同一目录中使用test.xml
我尝试过,同样的问题已经解决
you can try to change this path like
../../test.xml
or in same directory usetest.xml
i tried and the same problem is solved
如果要执行文件 I/O,则传入实际路径(使用 $_SERVER['DOCUMENT_ROOT'] 可能有助于指定这些路径)。 PHP 的
fopen
支持对 URL 的特殊处理,但其他文件 I/O 函数的包装器不支持。If you want to perform File I/O, then pass in actual paths (using
$_SERVER['DOCUMENT_ROOT']
may be helpful to specify those). PHP'sfopen
supports special handling for URLs, but the wrappers for other file I/O functions do not.