Solved

Reading files using bzread

Posted on 2003-11-11
6
338 Views
Last Modified: 2006-11-17
I'm trying to read a bz2 compressed file using the bzopen and bzread commands.  My problem is how do I tell bzread what size the file is so it knows to read to the end?  If I use the standard filesize($filename) command it returns the compressed size so the whole file isn't displayed.  All of my files vary wildly in size so it's not feasible to just hard code the value.  Here is what I have so far.

<html>
<body>
<font size="2">
<pre>
 <?php
$filename=$_GET["report_name"];
$file_content = bzread(bzopen($filename, "r"), filesize($filename));
echo $file_content;
?>
</pre>
</font>
</body>
</html>

Thanks in advance.
Chris
0
Comment
Question by:spiderman-55
  • 2
  • 2
6 Comments
 
LVL 13

Expert Comment

by:lozloz
ID: 9726023
hi,

why don't you try doing this:

<html>
<body>
<font size="2">
<pre>
<?
$file_content = "";
$content = "";
$filename=$_GET["report_name"];
$bz = bzopen($filename, "r");
while($true) {
  $file_content = bzread($bz, 8192);
  if (strlen($file_content) == 0) {
      break;
  }
  $content .= $file_content;
}
echo $content;
?>
</pre>
</font>
</body>
</html>

loz
0
 

Author Comment

by:spiderman-55
ID: 9751006
loz,
Thanks for responding but after cutting and pasting the above code all I get is a blank screen when run.  I have noticed that at the end of each file are two characters  I'm not sure what they are but they are at the end of every report before some unreadable stuff.  If I can somehow stop reading the file when I reach these two characters than I think I'll be golden.  Any thoughts?

Chris
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9751509
try this:

<html>
<body>
<font size="2">
<pre>
<?
$content = "";
$filename=$_GET["report_name"];
$bz = bzopen($filename, "r");
while (!feof($bz)) {
    $content .= bzread($bz, 4096);
}
echo $content;
bzclose($bz);
?>
</pre>
</font>
</body>
</html>
0
 

Author Comment

by:spiderman-55
ID: 9826152
Thanks for the suggestions but what I ended up doing was invoking bzcat to a temp folder and then getting the filesize from that.  Kinda rough but works like a champ.  Here is the code.

<html>
<body>
<font size="2">
<pre>
 <?php
//create a random file number
srand((double)microtime()*1000000);
$randval = rand();

//Get filename from displayindex.php
$filename="/directory/" . $_GET["report_name"];

//set temp file name to random file number
$tempfilename = "/temp_directory/" . $randval;

//this extracts the compressed file so we can get the filesize
exec ("bzcat -c " . $filename . " > ". $tempfilename);

//this gets the filesize from uncompressed file
$file_content = bzread(bzopen($filename, "r"),filesize($tempfilename));

//this strips the unwanted charcters in our report to make it all pretty like.
$file_content = str_replace(' ', '', $file_content);
$file_content = str_replace('', '', $file_content);

//this echos the file content to the screen
echo $file_content;

//this removes the tempfile
exec ("rm ". $tempfilename);


?>
</pre>
</font>
</body>
</html>
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12573090
PAQed with points refunded (50)

modulo
Community Support Moderator
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now