Solved

Reading files using bzread

Posted on 2003-11-11
6
341 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

863 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

20 Experts available now in Live!

Get 1:1 Help Now