Link to home
Start Free TrialLog in
Avatar of spiderman-55
spiderman-55

asked on

Reading files using bzread

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
Avatar of lozloz
lozloz

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
Avatar of spiderman-55

ASKER

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
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>
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>
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial