Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 24th, 2008, 5:07 PM   #1
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Question 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:
PHP Syntax (Toggle Plain Text)
  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:
PHP Syntax (Toggle Plain Text)
  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?
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old May 24th, 2008, 8:52 PM   #2
Apophis
Newbie
 
Join Date: Apr 2008
Posts: 16
Rep Power: 0 Apophis is on a distinguished road
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');
Apophis is offline   Reply With Quote
Old May 24th, 2008, 10:12 PM   #3
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Re: Strange fopen Problem

I'm not attempting to add more. I'm attempting to read that which has already been written.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old May 25th, 2008, 2:25 AM   #4
Apophis
Newbie
 
Join Date: Apr 2008
Posts: 16
Rep Power: 0 Apophis is on a distinguished road
Re: Strange fopen Problem

then your code should be:
php Syntax (Toggle Plain Text)
  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>
Apophis is offline   Reply With Quote
Old May 25th, 2008, 6:44 AM   #5
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 824
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange Problem with List STLFBr1 Java 1 Mar 7th, 2008 9:04 AM
Problem solving ReggaetonKing Software Design and Algorithms 7 Jan 4th, 2008 1:49 PM
Strange getMonth/setMonth problem aaroncampbell JavaScript and Client-Side Browser Scripting 4 Oct 31st, 2006 2:49 PM
strange problem brad sue C++ 3 Sep 1st, 2006 8:50 AM
Strange Problem? magic_e PHP 10 Feb 9th, 2006 8:54 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:26 AM.

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