Problem with fread()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rendl42@gmail.com

    Problem with fread()

    Hey all,

    I'm a relative newbie to PHP, and am getting some strange results with
    the fread() function with PHP (win32) 5.1.2. Here is some code:

    <?php
    echo $_SERVER['PHP_SELF']."<br>";
    echo $_SERVER['DOCUMENT_ROOT'];
    $myFile = $_SERVER['DOCUMENT_ROOT']."/Dave/blah.txt";
    echo $myfile."<br>";
    if (file_exists($m yFile)) {
    $fh = fopen($myFile, 'r');
    // $theData = fread($fh, filesize($my_fi le));
    $theData = fread($fh, 282);
    fclose($fh);
    echo $theData;
    } else {
    echo "<br>"." The file";
    echo $_SERVER['DOCUMENT_ROOT'];
    echo "$myFile does not exist";
    }
    ?>

    Now when I run the script using the commented out line for setting
    $theData variable, i get a file cannot be 0 bytes message (see error
    message at bottom). The file however is not empty. When I replace that
    commented line with the line below, I am able to echo $theData
    successfuly. I have heard that perhaps there is a bug with fread() in
    5.1.x (not sure which rev), but this is causing major pain.

    Am I doing something wrong? If this is a bug, is there a simple
    alternate I can use?

    Thanks heaps!


    Warning: fread() [function.fread]: Length parameter must be greater
    than 0 in C:\server\wamp\ htdocs\dodger\b lah.php on line 9

  • rendl42@gmail.com

    #2
    Re: Problem with fread()

    Just to add to that. The error message at the bottom says
    \dodger\blah.ph p and the top says \Dave\blah.php but in actual fact
    this was a manual edit of the error output. Both are set to
    \Dave\blah.php in reality.

    Comment

    • rendl42@gmail.com

      #3
      Re: Problem with fread()

      Fixed it finally. I found using relative path in setting $myFile
      variable was breaking it. An absolute path fixed it.

      Comment

      • d

        #4
        Re: Problem with fread()

        <rendl42@gmail. com> wrote in message
        news:1138880313 .657493.280900@ o13g2000cwo.goo glegroups.com.. .[color=blue]
        > Hey all,
        >
        > I'm a relative newbie to PHP, and am getting some strange results with
        > the fread() function with PHP (win32) 5.1.2. Here is some code:
        >
        > <?php
        > echo $_SERVER['PHP_SELF']."<br>";
        > echo $_SERVER['DOCUMENT_ROOT'];
        > $myFile = $_SERVER['DOCUMENT_ROOT']."/Dave/blah.txt";
        > echo $myfile."<br>";
        > if (file_exists($m yFile)) {
        > $fh = fopen($myFile, 'r');
        > // $theData = fread($fh, filesize($my_fi le));
        > $theData = fread($fh, 282);
        > fclose($fh);
        > echo $theData;
        > } else {
        > echo "<br>"." The file";
        > echo $_SERVER['DOCUMENT_ROOT'];
        > echo "$myFile does not exist";
        > }
        > ?>
        >
        > Now when I run the script using the commented out line for setting
        > $theData variable, i get a file cannot be 0 bytes message (see error
        > message at bottom). The file however is not empty. When I replace that
        > commented line with the line below, I am able to echo $theData
        > successfuly. I have heard that perhaps there is a bug with fread() in
        > 5.1.x (not sure which rev), but this is causing major pain.
        >
        > Am I doing something wrong? If this is a bug, is there a simple
        > alternate I can use?
        >
        > Thanks heaps!
        >
        >
        > Warning: fread() [function.fread]: Length parameter must be greater
        > than 0 in C:\server\wamp\ htdocs\dodger\b lah.php on line 9[/color]

        You're inter-changing $myfile, $myFile and $my_file - that might not help
        the situation ;)


        Comment

        • Jim Michaels

          #5
          Re: Problem with fread()


          <rendl42@gmail. com> wrote in message
          news:1138880313 .657493.280900@ o13g2000cwo.goo glegroups.com.. .[color=blue]
          > Hey all,
          >
          > I'm a relative newbie to PHP, and am getting some strange results with
          > the fread() function with PHP (win32) 5.1.2. Here is some code:
          >
          > <?php
          > echo $_SERVER['PHP_SELF']."<br>";
          > echo $_SERVER['DOCUMENT_ROOT'];
          > $myFile = $_SERVER['DOCUMENT_ROOT']."/Dave/blah.txt";
          > echo $myfile."<br>";[/color]

          you might get farther if you concatenate instead of assign. (below) You
          will also need to initialize the buffer if you do so. I also suggest using
          a slightly larger buffer for performance. between 2K-8K is good, but no more
          or you get truncation
          ..
          $theData="";
          [color=blue]
          > if (file_exists($m yFile)) {
          > $fh = fopen($myFile, 'r');
          > // $theData = fread($fh, filesize($my_fi le));
          > $theData = fread($fh, 282);[/color]

          $theData .= fread($fh, 8192);

          [color=blue]
          > fclose($fh);
          > echo $theData;
          > } else {
          > echo "<br>"." The file";
          > echo $_SERVER['DOCUMENT_ROOT'];
          > echo "$myFile does not exist";
          > }
          > ?>
          >
          > Now when I run the script using the commented out line for setting
          > $theData variable, i get a file cannot be 0 bytes message (see error
          > message at bottom). The file however is not empty. When I replace that
          > commented line with the line below, I am able to echo $theData
          > successfuly. I have heard that perhaps there is a bug with fread() in
          > 5.1.x (not sure which rev), but this is causing major pain.
          >
          > Am I doing something wrong? If this is a bug, is there a simple
          > alternate I can use?
          >
          > Thanks heaps!
          >
          >
          > Warning: fread() [function.fread]: Length parameter must be greater
          > than 0 in C:\server\wamp\ htdocs\dodger\b lah.php on line 9
          >[/color]


          Comment

          Working...