SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2000
    0 Post(s)
    0 Thread(s)

    I was wondering what the difference was between the file en the fopen function. I always use the file function to read a file, like this:

    $data = file('file.txt');

    I know that I can also use the fopen function to do the same, but what are the difference between these two function? Which one is the best to use?

  2. #2
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Melbourne, Australia
    2 Post(s)
    0 Thread(s)
    file() loads the entire contents of the specified file into an array, with each line of the file becoming one array element.

    fopen() allows you to open a file for reading or writing, so that you can then use fread() or fwrite() to read/write specific portions of the file, for example. When you're done with the file, you should call fclose() to close it and make it available to other programs/PHP scripts again.

    file() is very convenient for grabbing the whole file at once, but if you only need a couple of lines out of a very large file it can be very wasteful. file() also doesn't let you write the file.

    -Kevin Yank.
    Helping Small Business Grow Online!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts