By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,637 Members | 3,219 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,637 IT Pros & Developers. It's quick & easy.

fopen( )

P: n/a
I'm a newbie at this... I'm trying to run a PHP script from the W3C PHP
tutorial, and the example shows the following code:

<html>
<body>

<?php
$file=fopen("welcome.txt","r");
?>

</body>
</html>

I have a file in that directory labeled welcome.txt (with some text, of
course), but when I open the sample file (labeled fileOpen.php) in the
browser, it brings up a blank page (with no error message). I've been
working through the rest of the tutorial OK, and I know my web host
supports PHP, so my question is: What is this function supposed to do?
Bring up the text file in the browser? or open it in Notepad? Do I need
to combine it with another command to make it do anything?

Sorry if this is a stupid question. As I said, I'm new at this, but I
haven't had any trouble getting my scripts to work until now.

Help!

Jan 16 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 16 Jan 2007 15:43:52 -0800, "rfhurley" <rf******@yahoo.comwrote:
>I'm a newbie at this... I'm trying to run a PHP script from the W3C PHP
tutorial, and the example shows the following code:

<html>
<body>

<?php
$file=fopen("welcome.txt","r");
?>

</body>
</html>

I have a file in that directory labeled welcome.txt (with some text, of
course), but when I open the sample file (labeled fileOpen.php) in the
browser, it brings up a blank page (with no error message). I've been
working through the rest of the tutorial OK, and I know my web host
supports PHP, so my question is: What is this function supposed to do?
Bring up the text file in the browser? or open it in Notepad? Do I need
to combine it with another command to make it do anything?
fopen opens a file, and gives you a file handle that you can then use with
other functions that may read or write from or to that file. It won't output
anything on its own.

See for example http://uk2.php.net/manual/en/function.fread.php

If you just want to output the contents of a file, then there's:
http://uk2.php.net/manual/en/function.readfile.php

... and then the rest of the functions in that section of the manual.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Jan 17 '07 #2

P: n/a
Rik
rfhurley wrote:
I'm a newbie at this... I'm trying to run a PHP script from the W3C
PHP tutorial, and the example shows the following code:

<html>
<body>

<?php
$file=fopen("welcome.txt","r");
>>

</body>
</html>

I have a file in that directory labeled welcome.txt (with some text,
of course), but when I open the sample file (labeled fileOpen.php) in
the browser, it brings up a blank page (with no error message). I've
been working through the rest of the tutorial OK, and I know my web
host supports PHP, so my question is: What is this function supposed
to do? Bring up the text file in the browser? or open it in Notepad?
Do I need to combine it with another command to make it do anything?

Sorry if this is a stupid question. As I said, I'm new at this, but I
haven't had any trouble getting my scripts to work until now.
http://www.php.net/fopen

fopen() returns a resource (in essence a 'link' to the file), not the
actual contents.

Furthermore, you don't output anything to the browser, you just tell php to
open a file, and then.... nothing.

Following a fopen() is usually an fread() <http://www.php.net/fread>, and
to output stuff to the browser something like 'echo' or 'print' is used.
PHP cannot control your computer (i.e. open Notepad), it can only perform
actions on the server it runs on, and report back, usually to a browser by
http.

To get the contents of welcome.txt in your html page, you could include()
or require() it, you could use file_get_contents()/readfile() (essentially
a shortcut for fopen() & fread()) in combination with an echo, and several
other options.
--
Rik Wasmus
Jan 17 '07 #3

P: n/a
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)

Thanks again for the advice!

Rob
rfhurley wrote:
I'm a newbie at this, etc...

>
fopen() returns a resource (in essence a 'link' to the file), not the
actual contents.

Furthermore, you don't output anything to the browser, you just tell php to
open a file, and then.... nothing.

Following a fopen() is usually an fread() <http://www.php.net/fread>, and
to output stuff to the browser something like 'echo' or 'print' is used.
PHP cannot control your computer (i.e. open Notepad), it can only perform
actions on the server it runs on, and report back, usually to a browser by
http.

To get the contents of welcome.txt in your html page, you could include()
or require() it, you could use file_get_contents()/readfile() (essentially
a shortcut for fopen() & fread()) in combination with an echo, and several
other options.
--
Rik Wasmus
Jan 17 '07 #4

P: n/a
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ĝ
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Jan 17 '07 #5

P: n/a
I used this script (even tried a cut 'n' paste):

// 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;
this didn't work. And I tried (again, I tried it a second time, cutting
& pasting from your example):

echo file_read_contents("welcome.txt");
the "readfile()" example worked, although it ran all the lines into one
line. But finally I found the "fgets()" function (using the "feof()"
function to read line-by-line), and that printed out the text file the
way I wrote it. Is there some version-related caveat to the "contents"
functions that may affect whether they work or not over certain
servers?
Any 411 on that would be helpful.

Thanks again!

Rob

Kim André Akerĝ wrote:

>
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ĝ
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Jan 18 '07 #6

P: n/a
rfhurley wrote:
I used this script (even tried a cut 'n' paste):

// 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;
this didn't work. And I tried (again, I tried it a second time,
cutting & pasting from your example):

echo file_read_contents("welcome.txt");
the "readfile()" example worked, although it ran all the lines into
one line. But finally I found the "fgets()" function (using the
"feof()" function to read line-by-line), and that printed out the
text file the way I wrote it. Is there some version-related caveat to
the "contents" functions that may affect whether they work or not
over certain servers?
Any 411 on that would be helpful.

Thanks again!
If you're outputting this to a HTML page, you might want to do this:

echo nl2br(file_read_contents("welcome.txt"));

--
Kim André Akerĝ
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Jan 18 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.