Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   Strange fopen Problem (http://www.programmingforums.org/showthread.php?t=15876)

grimpirate May 24th, 2008 6:07 PM

Strange fopen Problem
 
I'd like to know why I'm getting the following error:
Quote:

0

Warning: fread() [function.fread]: Length parameter must be greater than 0 in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\blah.php on line 22
with this code:
:

  1. <pre>
  2. <?php
  3. $filename = 'foo.txt';
  4.  
  5. $h1 = fopen($filename, 'xb');
  6. fwrite($h1, 'bar');
  7. //$h2 = fopen($filename, 'rb');
  8. //echo fread($h2, filesize($filename));
  9. //fclose($h2);
  10. echo file_retrieve_contents($filename);
  11. fclose($h1);
  12. unlink($filename);
  13.  
  14. function file_retrieve_contents($filename, $offset = 0, $bytes = null){
  15.         $fileSize = filesize($filename);
  16.         echo $fileSize;
  17.         if(!is_int($offset)) $offset = 0;
  18.         if(!is_int($bytes)) $bytes = $fileSize - $offset;
  19.  
  20.         $handle = @fopen($filename, "rb");
  21.         fseek($handle, $offset);
  22.         $data = fread($handle, $bytes);
  23.         fclose($handle);
  24.         return $data;
  25. }
  26. ?>
  27. </pre>

And actually getting the output:
Quote:

bar
when I do the following:
:

  1. <pre>
  2. <?php
  3. $filename = 'foo.txt';
  4.  
  5. $h1 = fopen($filename, 'xb');
  6. fwrite($h1, 'bar');
  7. $h2 = fopen($filename, 'rb');
  8. echo fread($h2, filesize($filename));
  9. fclose($h2);
  10. //echo file_retrieve_contents($filename);
  11. fclose($h1);
  12. unlink($filename);
  13.  
  14. function file_retrieve_contents($filename, $offset = 0, $bytes = null){
  15.         $fileSize = filesize($filename);
  16.         echo $fileSize;
  17.         if(!is_int($offset)) $offset = 0;
  18.         if(!is_int($bytes)) $bytes = $fileSize - $offset;
  19.  
  20.         $handle = @fopen($filename, "rb");
  21.         fseek($handle, $offset);
  22.         $data = fread($handle, $bytes);
  23.         fclose($handle);
  24.         return $data;
  25. }
  26. ?>
  27. </pre>

Anyone have any ideas?

Apophis May 24th, 2008 9:52 PM

Re: Strange fopen Problem
 
With the second code you tell it to do a write to the file
:

fwrite($h1, 'bar');
which will then give it the string 'bar'.
If you are trying to get it to add more text in to the file try:
:

$h1=fopen($filename, 'a');

grimpirate May 24th, 2008 11:12 PM

Re: Strange fopen Problem
 
I'm not attempting to add more. I'm attempting to read that which has already been written.

Apophis May 25th, 2008 3:25 AM

Re: Strange fopen Problem
 
then your code should be:
:

  1. <pre>
  2. <?php
  3.  
  4. $filename = 'foo.txt';
  5. $h2 = fopen($filename, 'rb');
  6. echo fread($h2, filesize($filename));
  7. fclose($h2);
  8. //echo file_retrieve_contents($filename);
  9. fclose($h1);
  10. unlink($filename);
  11.  
  12. function file_retrieve_contents($filename, $offset = 0, $bytes = null){
  13. $fileSize = filesize($filename);
  14. echo $fileSize;
  15. if(!is_int($offset)) $offset = 0;
  16. if(!is_int($bytes)) $bytes = $fileSize - $offset;
  17. $handle = @fopen($filename, "rb");
  18. fseek($handle, $offset);
  19. $data = fread($handle, $bytes);
  20. fclose($handle);
  21. return $data;
  22. }
  23.  
  24. ?></pre>


The Dark May 25th, 2008 7:44 AM

Re: Strange fopen Problem
 
In the first one your are getting the file size before you open the file again, in the second you open the file for reading before you get the file size. I suspect there that the write isn't being flushed to disk until a different IO is performed, so the file's size on disk doesn't match what you think it does.


All times are GMT -5. The time now is 4:19 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC