SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    My true identity MaxS's Avatar
    Join Date
    Nov 2004
    Location
    New York
    Posts
    768
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Fwrite vs file_put_contents

    Hi,

    Which one would be faster and easier on the server? I have a script that rights to a file ever second or so, and when constantly done it empties the file.

    Someone suggested switching over to file_put_contents?

    Thank you for your input.

  2. #2
    Wadge! F4nat1c's Avatar
    Join Date
    Oct 2005
    Location
    South Wales, UK
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My preference when coding is file_put_contents.
    OMFG SitePoint ROXORZ TEH BIG ONE111!
    Wish you were invisible?

  3. #3
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    According to the manual file_put_contents() is "Identical to calling fopen(), fwrite(), and fclose() successively."... so I suppose if you are writing to the file several times in the same script fwrite() would be better, as it doesn't have to keep opening and closing the file. If you just need to write once then they are identical

  4. #4
    My true identity MaxS's Avatar
    Join Date
    Nov 2004
    Location
    New York
    Posts
    768
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gavwvin
    According to the manual file_put_contents() is "Identical to calling fopen(), fwrite(), and fclose() successively."... so I suppose if you are writing to the file several times in the same script fwrite() would be better, as it doesn't have to keep opening and closing the file. If you just need to write once then they are identical
    That's unfortunate

  5. #5
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MaxS
    That's unfortunate
    You might want to do some research and see if file_put_contents() caches connections and only closes them at the end. My experience with file_get_contents() is that fopen(), fread(), and fclose() is slightly faster -- though I have no clue why and it really shouldn't be.
    Christopher

  6. #6
    Wadge! F4nat1c's Avatar
    Join Date
    Oct 2005
    Location
    South Wales, UK
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gavwvin
    According to the manual file_put_contents() is "Identical to calling fopen(), fwrite(), and fclose() successively."... so I suppose if you are writing to the file several times in the same script fwrite() would be better, as it doesn't have to keep opening and closing the file. If you just need to write once then they are identical
    Sorry. I didn't realise he would be writing to the file consectuively
    OMFG SitePoint ROXORZ TEH BIG ONE111!
    Wish you were invisible?

  7. #7
    My true identity MaxS's Avatar
    Join Date
    Nov 2004
    Location
    New York
    Posts
    768
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arborint
    You might want to do some research and see if file_put_contents() caches connections and only closes them at the end. My experience with file_get_contents() is that fopen(), fread(), and fclose() is slightly faster -- though I have no clue why and it really shouldn't be.
    I don't see how caching the connection would help considering hundreds of files are being written to, perhaps 5-10 a second.

  8. #8
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MaxS
    I don't see how caching the connection would help considering hundreds of files are being written to, perhaps 5-10 a second.
    PHP may not close the file until the script ends or some other condition is met, such as too many files open. So succeeding calls to file_put_contents() may not need to do an fopen(). This is only conjecture on my part though. But I know that PHP tries internally to be smart when dealing with files.
    Christopher

  9. #9
    My true identity MaxS's Avatar
    Join Date
    Nov 2004
    Location
    New York
    Posts
    768
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's opening different files, though. Therefore keeping the file open would not be an option.

    Thanks

  10. #10
    My true identity MaxS's Avatar
    Join Date
    Nov 2004
    Location
    New York
    Posts
    768
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So really, the only way to run successfully a script like this would be to move to a dedicated server?

  11. #11
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if its writing to different files then fopen() fwrite() and fclose() and file_put_contents() are identical: php.net/file_put_contents


Bookmarks

Posting Permissions

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