Forum Moderators: coopster

Message Too Old, No Replies

write to file not working

         

StickeR

9:33 pm on Aug 21, 2006 (gmt 0)

10+ Year Member



Hiya folks,
anyone got some info for me on writing to files using fwrite?
I have the script, but my server isn't letting me do what I want I keep getting the following error:

------
Warning: fopen(files/testFile.txt): failed to open stream: File exists in ***/tests/fc.php on line 7
Warning: fwrite(): supplied argument is not a valid stream resource in ***/tests/fc.php on line 9
Warning: fclose(): supplied argument is not a valid stream resource in ***/tests/fc.php on line 11
Warning: fopen(files/testFile.txt): failed to open stream: Permission denied in ***/tests/fc.php on line 15
------
I tried it, no luck, then I tried it after having my files folder Chmodded to 777,but still no luck.
The file exists, but something wont happen, andI don't know what or how.

Advice / tips / solutions on this is greatly appreciated

[edited by: StickeR at 9:34 pm (utc) on Aug. 21, 2006]

BarryStCyr

9:40 pm on Aug 21, 2006 (gmt 0)

10+ Year Member



What is the file chmod? The file will have to be 777 as well.

Is the file accessible directly as a URL from you website. This has nothing to do with the problem at hand, but it can be a security problem. Someone could put some kind of mal-ware in this file if it is accessible as a URL.

Hope this helps.

StickeR

11:23 pm on Aug 21, 2006 (gmt 0)

10+ Year Member



Both the folder and file are Chmodded as 777.
I can't get it to work at all.

And no the file is just a seperate personal file in my testing folder, I always test this stuff before making it public :) .

jatar_k

5:23 pm on Aug 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the fopen error is the one to fix, the rest derive from the fact that it failed.

what mode are you using in your fopen call?

StickeR

8:39 pm on Aug 22, 2006 (gmt 0)

10+ Year Member



dunno if this helps, but this is the script I'm using, got it from a tutorial somwewhere:

<?php
$filename = 'files/testFile.txt';
$Content = "Add this to the file\r\n";
echo "open";
$handle = fopen($filename, 'x+');
echo " write";
fwrite($handle, $Content);
echo " close";
fclose($handle);

if($handle = fopen($filename, 'a')){
if(is_writable($filename)){
if(fwrite($handle, $content) === FALSE){
echo "Cannot write to file $filename";
exit;
}
echo "The file $filename was created and written successfully!";
fclose($handle);
} else{
echo "The file $filename, could not written to!";
exit;
}
} else{
echo "The file $filename, could not be created!";
exit;
};
?>

jatar_k

10:25 pm on Aug 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



from the manual about using the 'x+' mode

[ww.php.net...]

Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL¦O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files.

so it is doing exactly as it is supposed to.

if you change this line
$handle = fopen($filename, 'x+');

to use a different mode, depending on what you want to do with the file, then it should work