You are here: Re: fopen( ) « PHP Programming Language « IT news, forums, messages
Re: fopen( )

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]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация