View Full Version : Writing to files and retrieving the value
pcbrainbuster
03-11-2007, 03:00 PM
Hello :), heres my script and i don't understand what's wrong with it -
<html>
<body>
<?php
$file="file.txt";
$fileo = fopen($file,"a");
$filew = fwrite($fileo,"Rocks Are Cool");
$filer = fread($fileo);
$filec = fclose($file);
echo $filer;
?>
</body>
</html>
So whats wrong with it ?
pcbrainbuster
03-11-2007, 09:22 PM
Cmon guys i really don't mind havin some help here :)
boxxertrumps
03-11-2007, 09:26 PM
What error messages do you get?
pcbrainbuster
03-11-2007, 09:26 PM
It simply shows nothing on the page...
mburt
03-11-2007, 09:27 PM
Replace fread (which returns a handle) with file_get_contents.
pcbrainbuster
03-11-2007, 09:28 PM
Whats the difference ?
boxxertrumps
03-11-2007, 09:30 PM
not sure. but it always seems to work better than fread.
pcbrainbuster
03-11-2007, 09:30 PM
What do you mean ?
boxxertrumps
03-11-2007, 09:32 PM
I Mean, use this...
<html>
<body>
<?php
$file="file.txt";
$fileo = fopen($file,"a");
fwrite($fileo,"Rocks Are Cool");
fclose($file);
$filer = file_get_contents($file);
echo $filer;
?>
</body>
</html>
pcbrainbuster
03-11-2007, 09:34 PM
Ok give me some time to work it out (10 min) - while then tell me did that mean that all that time i made only one mistake...?
mburt
03-11-2007, 09:35 PM
Also, you're trying to fclose() a string. Should be:
<html>
<body>
<?php
$file="file.txt";
$fileo = fopen($file,"a");
fwrite($fileo,"Rocks Are Cool");
fclose($fileo);
$filer = file_get_contents($file);
echo $filer;
?>
</body>
</html>
boxxertrumps
03-11-2007, 09:37 PM
oops, should be more carefull what i give people...
pcbrainbuster
03-11-2007, 09:39 PM
Just saying but i tested boxertrumps script first and there is nothing wrong with it... why do i need to do mburt's script (Whats the difference ?)
boxxertrumps
03-11-2007, 10:21 PM
i accidentally fclosed a string instead of the file handle.
pcbrainbuster
03-11-2007, 10:26 PM
please give examples on evrthing you are saying also explain please on what is a file handle (i think its fopen(rest) related)
boxxertrumps
03-11-2007, 10:35 PM
$fileo = fopen($file,"a");
$fileo is a file handle.
"file.txt" is a string.
you can only close handles, not strings.
which i the script tried to do here
fclose($file);
pcbrainbuster
03-12-2007, 07:53 AM
Ok i see thanks ! :)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.