Forum Moderators: coopster

Message Too Old, No Replies

Creating new files in windows

file creation fails

         

ergophobe

8:01 pm on Jul 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Under Linux, I don't seem to have a problem creating new files with standard commands:

$handle = fopen($file, 'w');
fwrite($handle, $content);

That works fine on Linux, but on Windows I always have to create an empty file in some other way and then I can write to it.

Is this a matter of directory permissions or what?

Tom

coopster

8:18 pm on Jul 22, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



This works just fine on my XP/Apache/PHP box, but I'm running it as an Administrator.

What does

$file
look like? I mean, is it like
'/dir/subir/file.txt'
or is it a URL? Also, have you tried adding the 'b' flag to force binary mode? To use this flag, specify the 'b' as the last character of the mode parameter.
$handle = fopen($file, 'wb');

ergophobe

8:48 pm on Jul 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Forget to mention that I'm on Win2K. I'm running it as Admin, but then I don't know what privileges PHP has.

It doesn't really matter what the file name is, but I've tried both in and out of web root, slashes and backslashes, long and short file names. I just can't create files under Windows.

1. The scripts creates files fine under Linux.
2. If I create an empty file, the script writes to it fine under Windows.

Conclusion: the script can create a files (as under Linux) and the Windows path is correct (since once created, the script overwrites the file fine). But it just doesn't like to let me create a file.

I think it's some weird Windows permission thing that I don't understand.

coopster

9:16 pm on Jul 22, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The reason I was asking about the file name was to determine how you were binding the named resource to a stream -- as a regular file or a wrapper for the http protocol. But that point is obviously not even relative here since the http wrapper doesn't support "write-type" connections. You have to be naming a regular file. That cuts that brainstorming back quickly now, doesn't it?

So you are saying you think it is a Windows "create" file permission issue, correct? Well, if the

fopen()
fails, the function returns
FALSE
and an error of level
E_WARNING
is generated. You've got
error_reporting(E_ALL);
on right?

coopster

9:45 pm on Jul 22, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well, this is going to be a fun one, isn't it? Try something along these lines to see if we can't narrow down the issue...
<?php 
echo 'safe_mode is: ' . ini_get('safe_mode') . "<br />\n";
echo 'open_basedir is: ' . ini_get('open_basedir') . "<br />\n";
error_reporting(E_ALL);
$file = 'fwrite.txt';
$content = "Line 1\nLine 2\nLine 3\n";
if ($handle = fopen($file, 'wb+')) {
if (($bytes = fwrite($handle, $content)) === false) echo 'fwrite() problem!';
} else {
echo 'fopen() problem!';
}
echo "Success! Bytes written: $bytes";
?>

ergophobe

9:55 pm on Jul 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Now I'm embarassed. After repeatedly having trouble with this, I can't reproduce it all the sudden. I'll have to wait until it comes up again and be more attentive the to exact circumstances.

I even went back to a couple of the trouble scripts and deleted the files they were having trouble creating and those scripts work now. I must have changed some Windows setting (scripts are unchanged!). I'm perplexed, though, since I'm logged in as the same user.

Sorry for the wasted bandwidth....

Tom