Solved

Reading files using bzread

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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 count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

831 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