Why would is_readable & is_writable & file_exists fail for URL path and not for a file in the same directory?
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开发者_JAVA技巧 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.
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
$filename = "//127.0.0.1/sitename/admin/interface/test.xml";
you can try to change this path like ../../test.xml
or in same directory use test.xml
i tried and the same problem is solved
If you want to perform File I/O, then pass in actual paths (using $_SERVER['DOCUMENT_ROOT']
may be helpful to specify those). PHP's fopen
supports special handling for URLs, but the wrappers for other file I/O functions do not.
精彩评论