open_basedir won't let me write file, but directory seems correct?
Short story:
I'm getting an open_basedir restriction in my php script - a simple "test writing a new file to disk" script. It SEEMS to me that I've got the open_basedir settings correct and the file is in the right location - but no luck, just the same error every time. I've searched for similar open_basedir problems on this site, but I've not seen any with this problem - where the directory looks right but it still throws errors.
My guesses as to what the problem is:
1) open_basedir doesn't work the way I think it does 2) My settings are wrong and I'm just not seeing it 3) It's actually something else, like IIS read/write permissions, etc 4) ???Long story:
I'm working on an IIS server with PHP and I'm trying to get the following code snippet to work (a simple write file test):
date_default_timezone_s开发者_StackOverflowet('America/Chicago');
$myFile = 'testfile.txt';
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Some Text\n";
fwrite($fh, $stringData);
$stringData = "Some More Text\n";
fwrite($fh, $stringData);
fclose($fh);
This php script is located at C:\inetpub\wwwroot\WEBDIRECTORY\test_write.php on my server
Here is my php.ini setting for open_basedir:
open_basedir = c:\inetpub\wwwroot;c:\inetpub\wwwroot\WEBDIRECTORYWhenever I open the script's page, I am expecting to see no output, and then there should be a new file written on the server when I check back. This is what I get instead:
Warning: fopen(): open_basedir restriction in effect. File(testfile.txt) is not within the allowed path(s): (c:\inetpub\wwwroot) in C:\inetpub\wwwroot\WEBDIRECTORY\test_write.php on line 5 Warning: fopen(testfile.txt): failed to open stream: Operation not permitted in C:\inetpub\wwwroot\WEBDIRECTORY\test_write.php on line 5 can't open file
I've tried a lot of permutations: originally the open_basedir line was just
open_basedir = c:\inetpub\wwwroot...but that didn't work, either.
I've also tried entering an absolute path for testfile.txt (c:\inetpub\wwwroot\WEBDIRECTORY\testfile.txt, etc) instead of just the name of the file itself, and I keep getting the same error message.
Any ideas? Thanks so much for your help.
Okay, so sorry to waste everyone's time - turns out it was a server setting my system administrator had set for me. (I'm new to this so I'll explain in general terms that I understand)
He'd set it up so that the server would not resolve an address for any files in the web directory except certain types you expect to find on a website, such as .htm, .html, .php, .jpg, etc. The file I was trying to read was .txt.
So, I wrote some test code:
$test = realpath('c:\inetpub\wwwroot\WEBDIRECTORY\testfile.php');
echo ("test = $test\n");
if($test == FALSE){
echo ("... is FALSE\n");
}
$test = realpath('c:\inetpub\wwwroot\WEBDIRECTORY\testfile.txt');
echo ("test = $test\n");
if($test == FALSE){
echo ("... is FALSE\n");
}
Which promptly returned this:
test = C:\inetpub\wwwroot\WEBDIRECTORY\testfile.php
test = ... is FALSE
So, realpath is refusing to return an address for anything ending in .txt, but is more than happy to do that for a .php file. This means that WHENEVER I put in an otherwise legal (within the basedir) filename that ends in an illegal extension, the server resolves that address not as "C:\inetpub\wwwroot\WEBDIRECTORY\testfile.txt" but as "". And of course, an empty string is not going to be in the basedir, returning open_basedir restriction errors.
BIZARRE! But makes sense now that I finally traced it back to its source. Thanks for all the tips! It helped me go through process of elimination and figure this one out.
It appears to be a permissions problem. Check the permissions on C:\inetpub\wwwroot\WEBDIRECTORY\ If special permissions are required sometimes you have to change the permissions on the .PHP script doing the writing. Typically you need both the directory and the script set, not just one or the other.
Here is a good script from the PHP manual for opening a file for writing:
$filename = 'test.txt'; $somecontent = "Add this to the file\n";
// Let's make sure the file exists and is writable first. if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else { echo "The file $filename is not writable"; }
精彩评论