Forum Moderators: coopster
$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
What does
$filelook 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');
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.
So you are saying you think it is a Windows "create" file permission issue, correct? Well, if the
fopen()fails, the function returns
FALSEand an error of level
E_WARNINGis generated. You've got
error_reporting(E_ALL);on right?
<?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";
?>
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