Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Flat files for php? 1

Status
Not open for further replies.

PhoenixDown

Programmer
Jul 2, 2001
279
CA
How do I do that? writing them and getting the data? Thanks.
 
Do you mean plain text files?

<?php
$test = &quot;\nAdd this line to somefile.txt&quot;;
$handle = fopen(&quot;somefile.txt&quot;, &quot;a&quot;);
fwrite($handle, $test);
fclose($handle);

$handle = fopen(&quot;somefile.txt&quot;, &quot;r&quot;);
while (!feof($handle)) {
$content .= fread($handle, 1000);
}
fclose($handle);
echo $content;
?>

BTW, have a look at for information about the &quot;r&quot;'s and &quot;a&quot;'s.

HTH, Sascha

If you just want to read the file, you can also use cu, Sascha
 
Thanks alot, but I get an error when I use my script.

Code:
Warning: Supplied argument is not a valid File-Handle resource in /test-php/my/calvin.php on line 10

Is it because somefile.txt did not have any file permissions? I thought PHP scripts didn't have to have file permissions to over write a file.
 
Line 10 is the fclose call after the while loop?

Strange error... But the file is created and the content is printed out as expected? cu, Sascha
 
yes.

if the file exists for a user you have to change the permitions of it to 777

chmod 777 file.txt

This way everybody can read/write/execute the file.

just another thing, when you open a file do something like this:

if(!($fp=fopen(...))){
echo &quot;Cannot open file&quot;;
exit;
}


This way, if you cannot open the file the program exits.
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top