views:

32

answers:

2

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_set('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\WEBDIRECTORY

Whenever 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.

A: 

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"; }

Jim
Hey! Thanks, I'm going to give that a try and let you know what happens!
larsiusprime
It is not. Permissions has nothing to do here
Col. Shrapnel
Okay, got this back when I used that test file:Warning: is_writable(): 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 6 The file testfile.txt is not writable
larsiusprime
Seems to say testfile.txt does not exist. try creating it and retesting. This code will test opening an existing file and appending to it.
Jim
Okay, did that - created "testfile.txt" and re-ran the script. Same message. I checked on the permissions, and it seems that IUSR, SYSTEM, and NETWORK SERVICE all have read,write,and execute permissions on testfile.txt, as well as on the script itself (test_write.php)
larsiusprime
A: 

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.

larsiusprime