SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard Crowe's Avatar
    Join Date
    Nov 2001
    Location
    Huntsville
    Posts
    1,117
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    fread vs file_get_contents

    I have a script that people are using and about half the people have reported that the following line doesn't work:

    PHP Code:
    $r file_get_contents($URL); 
    The other half say this version doesn't work:

    PHP Code:
    $r fread($file80000); 

    Just before that line I use

    PHP Code:
    $file fopen("$URL""r"); 
    I can't figure out why one method works fine for some people and the other works the rest of the time. Is there a method that will work 100% of the time?
    Chrispian H. Burks
    Nothing To Say

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    Michigan, USA
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is an odd problem. I'd try and get some system information on each user that is reporting problems and see if there are any trends.

    The only solution I can think of is to do one of the methods, and then if the variable contains no data, do the other method. It's kind of a hack, but it's better than nothing, and at least buys you some time to find a real fix.

    PHP Code:
    $file fopen($URL,'r');
        
    $r fread($file,80000); 
    fclose($file);
     
    if(!isset(
    $r))
        
    $r file_get_contents($URL); 

  3. #3
    SitePoint Wizard Crowe's Avatar
    Join Date
    Nov 2001
    Location
    Huntsville
    Posts
    1,117
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Not a bad idea. I think the problem is different php versions. file_get_contents is pretty new. I could just include my own file_get_contents() function and use fread in that function. I'll do a bit more investigating in the mean time.
    Chrispian H. Burks
    Nothing To Say

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Middle / East TN
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    According to the manual, file_get_contents() only works with version 4.3.0 and above. That's probably the problem. I'd say most people have 4.2 or 4.1.
    There are 10 kinds of people in this world,
    those who can read binary and those who can't.

  5. #5
    SitePoint Wizard Crowe's Avatar
    Join Date
    Nov 2001
    Location
    Huntsville
    Posts
    1,117
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Just wanted to post my solution in case anyone else runs into something similiar:

    PHP Code:
    if (function_exists('file_get_contents')) {
        
    $r file_get_contents($URL);  
    } else {
        
    $r fread($file80000);

    function_exists() rocks!
    Chrispian H. Burks
    Nothing To Say

  6. #6
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  7. #7
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Chrispian, here's a snippet I use a lot you may like.
    PHP Code:
    if (! function_exists(file_get_contents()) {
      function 
    file_get_contents($file) {
        if (! 
    $f fopen($file'r')) {
          return 
    FALSE;
        }
        
    $contents fread($ffilesize($file);
        
    fclose($file);
        return 
    $contents;
      }

    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure

  8. #8
    SitePoint Wizard Crowe's Avatar
    Join Date
    Nov 2001
    Location
    Huntsville
    Posts
    1,117
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by anode
    Chrispian, here's a snippet I use a lot you may like.
    PHP Code:
    if (! function_exists(file_get_contents()) {
      function 
    file_get_contents($file) {
        if (! 
    $f fopen($file'r')) {
          return 
    FALSE;
        }
        
    $contents fread($ffilesize($file);
        
    fclose($file);
        return 
    $contents;
      }


    That's pretty elegant, actually. I never thought of defining a function based on a conditional. Didn't even know it was possible! Now it makes all kinds of sense Great snippet!
    Chrispian H. Burks
    Nothing To Say


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
  •