|
Posted by Kim Andrι Akerψ on 01/17/07 06:49
rfhurley wrote:
> Thanks for writing. OK, I was able to use the include/require()
> commands; what I am wondering though, is in what context would you use
> the fopen() command as opposed to using the other commands you
> mentioned? The tutorial had nothing to say on that subject. Also, the
> fopen() command has "write" and "append" modes. How would this work?
>
> (Also, I tried using the "echo" command following the fopen() command:
>
> $file=fopen("welcome.txt","r");
> echo $file;
>
> It didn't work-- I presume because I wasn't using it correctly)
As Rik said, $file would only contain a link ("handle" or "reference")
to the file, not the contents of the file itself.
If you had read the manual page for fread(), you'd see an example of
how it's used.
In your case:
// open file
$file = fopen("welcome.txt", "r");
// fetch contents of file (using filesize() to fetch the entire file
// contents
$contents = fread($file, filesize($file));
// close file, since we don't need to read it further in this session
fclose($file);
// output contents
echo $contents;
And as Rik also pointed out, you can do this in one shot using
file_read_contents() or readfile():
echo file_read_contents("welcome.txt");
OR
readfile("welcome.txt");
--
Kim AndrΓ© AkerΓΈ
- kimandre@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Navigation:
[Reply to this message]
|