fopen( )

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rfhurley

    fopen( )

    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("we lcome.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!

  • Andy Hassall

    #2
    Re: fopen( )

    On 16 Jan 2007 15:43:52 -0800, "rfhurley" <rfhurley@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("w elcome.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:


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

    --
    Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

    Comment

    • Rik

      #3
      Re: fopen( )

      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("we lcome.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.


      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_conten ts()/readfile() (essentially
      a shortcut for fopen() & fread()) in combination with an echo, and several
      other options.
      --
      Rik Wasmus


      Comment

      • rfhurley

        #4
        Re: fopen( )

        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("we lcome.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_conten ts()/readfile() (essentially
        a shortcut for fopen() & fread()) in combination with an echo, and several
        other options.
        --
        Rik Wasmus

        Comment

        • =?iso-8859-1?Q?Kim_Andr=E9_Aker=F8?=

          #5
          Re: fopen( )

          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("we lcome.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_conte nts() or readfile():

          echo file_read_conte nts("welcome.tx t");

          OR

          readfile("welco me.txt");

          --
          Kim André Akerø
          - kimandre@NOSPAM betadome.com
          (remove NOSPAM to contact me directly)

          Comment

          • rfhurley

            #6
            Re: fopen( )

            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_conte nts("welcome.tx t");


            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_conte nts() or readfile():
            >
            echo file_read_conte nts("welcome.tx t");
            >
            OR
            >
            readfile("welco me.txt");
            >
            --
            Kim André Akerø
            - kimandre@NOSPAM betadome.com
            (remove NOSPAM to contact me directly)

            Comment

            • =?iso-8859-1?Q?Kim_Andr=E9_Aker=F8?=

              #7
              Re: fopen( )

              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_conte nts("welcome.tx t");
              >
              >
              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("welc ome.txt"));

              --
              Kim André Akerø
              - kimandre@NOSPAM betadome.com
              (remove NOSPAM to contact me directly)

              Comment

              Working...