Solved

Reading files using bzread

Posted on 2003-11-11
6
351 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

726 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