Whats the difference between fopen() function and the file() function ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeddiki
    Contributor
    • Jan 2009
    • 290

    Whats the difference between fopen() function and the file() function ?

    Hi,

    I am confused about whether I should be using just the
    file() function or both fopen() function and the file() function.

    I have a file that is a list of words in a text fle.

    Now I want to be able to use it with a php script so I want to get it into a mysql database.

    The file not comma separated it is just a simple list
    like this:

    athrong
    athrough
    athumia
    athwart
    athwarthawse
    athwartship
    athwartships

    How can I read these words and then insert them into a table ?

    I have read "the manual".
    From the description of the file() function in the manual, it
    looks like I can just put the file into an array and use it
    straight away.

    eg

    Code:
    while (( $data = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES))  !== FALSE) {
    do_stuff();
    }
    But if that is the case - what is fopen() for ??

    Can someone please help me understand this ?

    Thanks
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Using fopen() returns a resource that is used by other functions such as fgets(), feof(), fclose(), etc. So it's slightly more work using that route.

    file(), however, returns the file it reads as an array (each new line), making traversing a file much easier.

    Comment

    Working...