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 Oct 2009

view this page in

gzread

(PHP 4, PHP 5)

gzreadLiest aus einer gz-Datei

Beschreibung

string gzread ( resource $zp , int $length )

gzread() liest bis zu length Bytes aus der gegebenen gz-Datei Resource. Wird beim Lesen das Dateiende erreicht können auch weniger Bytes gelesen werden.

Parameter-Liste

zp

Die gz-Datei Resource. Dies muss auf eine gültige, erfolgreich mit gzopen() geöffnete Datei zeigen.

length

Die Anzahl der zu lesenden Bytes.

Rückgabewerte

Die gelesenen Daten.

Beispiele

Beispiel #1 gzread() Beispiel

<?php
// Liest den Inhalt einer gz-Datei in einen String
$filename "/usr/local/something.txt.gz";
$zd gzopen($filename"r");
$contents gzread($zd10000);
gzclose($zd);
?>

Siehe auch

  • gzwrite() - Ausgabe in gz-komprimierte Dateien
  • gzopen() - Öffnet gz-Dateien
  • gzgets() - Get line from file pointer
  • gzgetss() - Get line from gz-file pointer and strip HTML tags
  • gzfile() - Read entire gz-file into an array
  • gzpassthru() - Output all remaining data on a gz-file pointer



gzrewind> <gzputs
Last updated: Fri, 30 Oct 2009
 
add a note add a note User Contributed Notes
gzread
robinv12001 at yahoo dot co dot in
11-Jun-2009 06:21
<?php
//Extracting the content of a gzip file in php and creating a new csv file using the content
//Create a folder uploads inside current folder
$local_file    =    'test.gz';

//To Get the size of the uncompressed file
$FileRead = $local_file;
$FileOpen = fopen($FileRead, "rb");
fseek($FileOpen, -4, SEEK_END);
$buf = fread($FileOpen, 4);
$GZFileSize = end(unpack("V", $buf));
fclose($FileOpen);
//To Get the size of the zip file

$fzip_size    =    $GZFileSize;
   
$filename    =    $local_file;
$ext        =    pathinfo($filename,PATHINFO_EXTENSION);   
$file_name    =    pathinfo($filename,PATHINFO_FILENAME);       
$file_name    =    $file_name.".csv";

$zd         =     gzopen($filename, "rb");
$contents     =     gzread($zd, $fzip_size);
gzclose($zd);
$file_name    =    "uploads/".$file_name;
$fp    =    fopen($file_name, "wb");
fwrite($fp, $contents); //write contents of feed to cache file
fclose($fp);
if(
file_exists($file_name))
{
   
chmod($file_name,0755);
   
unlink($local_file);
}
?>
robinv12001 at yahoo dot co dot in
09-Jun-2009 11:18
<?php
//Using FTP Download a gzip file on current date from a remote server
//The gzip files are extracted
function parse_rawlist( $array )
{
   
$i=0;
    foreach(
$array as $curraw)
    {
       
$struc = array();
       
$current = preg_split("/[\s]+/",$curraw,9);

       
$struc['perms']      =     $current[0];
       
$struc['number']     =     $current[1];
       
$struc['owner']     =     $current[2];
       
$struc['group']      =     $current[3];
       
$struc['size']         =     $current[4];
       
$struc['month']      =     $current[5];
       
$struc['day']        =     $current[6];
       
$struc['time']      =     $current[7];
       
$struc['zfile_name']      =     $current[8];
    
$structure[$i]      =     $struc;
   
$i++;
    }
   return
$structure;

}
ini_set('memory_limit', '100M');

//Give FTP Information Here
//------------------------------------------------------------
$ftp_server     =     "example.com";
$ftp_user_name    =    "example";
$ftp_user_pass    =    "test";
//------------------------------------------------------------
//------------------------------------------------------------
$local_path     =    '/opt/lampp/htdocs/sample/test.gz';//Local Path
$server_path     =     'outgoing/folder/';//Remote Sever Path
//------------------------------------------------------------

$conn_id = ftp_connect($ftp_server,21);
if(
$conn_id)
{
    echo
"Successfully connected";
}
 
$login_result     =     ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_chdir($conn_id, $server_path);//Change Current Working Directory
$file_list     =    ftp_rawlist($conn_id,".");
$zip_details    =    parse_rawlist($file_list);
set_time_limit(1000);
$curr_date    =    strtotime(date("F d Y"));
for(
$i=0;$i<count($zip_details);$i++)
{
   
$fname        =    $zip_details[$i]['zfile_name'];   
   
$mod_date1    =    $zip_details[$i]['month']." ".$zip_details[$i]['day']." ".date("Y");   
   
$mod_date    =    strtotime($mod_date1);//File Modified Date
   
if($mod_date == $curr_date)//If modified date equals current date
   
{
       
$local_file    =    $fname;
       
$server_file    =    $fname;
        if (
ftp_get($conn_id, $local_file, $server_file, FTP_BINARY))
        {
       
            echo
"<br/>Successfully written to $local_file\n";
        }
        else
        {
            echo
"<br/>There was a problem\n";
        }
        if(
file_exists($local_file))
        {
           
$fzip_size    =    $zip_details[$i]['size'];   
           
$filename    =    $local_file;
           
$ext        =    pathinfo($filename,PATHINFO_EXTENSION);   
           
$file_name    =    pathinfo($filename,PATHINFO_FILENAME);       
           
$file_name    =    $file_name.".csv";
           
$zd         =     gzopen($filename, "r");
           
$contents     =     gzread($zd, $fzip_size);
           
$file_name    =    "uploads/".$file_name;
           
file_put_contents($file_name,addslashes($contents));
            if(
file_exists($file_name))
            {
               
chmod($file_name,0755);
               
unlink($local_file);
            }
           
gzclose($zd);
        }
    }
}
ftp_close($conn_id);
?>
utku
08-Feb-2008 10: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 12: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 09: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 Oct 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites