PHP
downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

gzrewind> <gzputs
Last updated: Fri, 30 Jan 2009

view this page in

gzread

(PHP 4, PHP 5)

gzreadバイナリ対応のgzファイル読み込み

説明

string gzread ( resource $zp , int $length )

gzread() は、最大 length バイトのデータを zp が指す gz ファイルポインタ から読み込みます。(解凍された) length バイトのデータが読み込まれたか、 EOF に達したとき、読み込みは終了します。

パラメータ

zp

gz ファイルポインタを指定します。これは有効なファイルポインタであり、 かつ、gzopen() によりオープンできたファイルを指している必要があります。

length

読み込むバイト数を指定します。

返り値

読み込まれたデータ

例1 gzread() の例

<?php
// gz ファイルの内容を文字列に読み込む
$filename "/usr/local/something.txt.gz";
$zd gzopen($filename"r");
$contents gzread($zd10000);
gzclose($zd);
?>

参考

  • gzwrite() - バイナリセーフな gz ファイル書き込み
  • gzopen() - gz ファイルを開く
  • gzgets() - ファイルポインタから 1 行を得る
  • gzgetss() - gzファイルへのポインタから1行を得て、HTMLタグを取り除く
  • gzfile() - gzファイル全体を配列に読み込む
  • gzpassthru() - gzファイルへのポインタから残りのデータ全部を出力する



gzrewind> <gzputs
Last updated: Fri, 30 Jan 2009
 
add a note add a note User Contributed Notes
gzread
utku
08-Feb-2008 11:31
I don't think it would be wise to open a whole gzip file (it _is_ compressed for a reason, and likely to be a very big file) into a single string. You would most likely hit the php memory limits. Instead, if you need to process the file, put your gzread calls in a loop, and read incrementally by a setting $length to a constant value.
zaotong at yahoo dot com
08-Mar-2007 01:06
As was shown to me in another forum there is a way to get the uncompressed size of the gz file by viewing the last 4 bytes of the gz file.

Here is a piece of code that will do this
<?php
$FileRead
= 'SomeText.txt';
$FileOpen = fopen($FileRead, "rb");
       
fseek($FileOpen, -4, SEEK_END);
       
$buf = fread($FileOpen, 4);
       
$GZFileSize = end(unpack("V", $buf));
       
fclose($FileOpen);
       
$HandleRead = gzopen($FileRead, "rb");
       
$ContentRead = gzread($HandleRead, $GZFileSize);
?>

This will read the last 4 bytes of the gz file and use it as the file int for the gzread.

Thanks to stereofrog for helping me with this code.
methatron at hotmail dot com
27-Sep-2006 10:33
Be aware that gzread's second parameter - length reffers to the file's uncompressed size, therefore using this code:

<?php
$file_name
= "/usr/local/something.txt.gz";
if(
$file_handle = gzopen($file_name, "r"))
{
   
$contents = gzread($file_handle, filesize($file_name));
   
gzclose($file_name);
}
?>

will probably truncate the content of the file as filesize checks for the file's compressed size.
So either use the actual uncompressed size, if you know it, or use an aribtrary big enough length, as gzreading will stop at the end of the file anyway.

gzrewind> <gzputs
Last updated: Fri, 30 Jan 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites