文字

gzfile

(PHP 4, PHP 5)

gzfileRead entire gz-file into an array

说明

array gzfile ( string $filename [, int $use_include_path = 0 ] )

This function is identical to readgzfile() , except that it returns the file in an array.

参数

filename

The file name.

use_include_path

You can set this optional parameter to 1, if you want to search for the file in the include_path too.

返回值

An array containing the file, one line per cell, empty lines included, and with newlines still attached.

范例

Example #1 gzfile() example

<?php
$lines 
gzfile ( 'somefile.gz' );
foreach (
$lines  as  $line ) {
    echo 
$line ;
}
?>

参见

  • readgzfile() - Output a gz-file
  • gzopen() - Open gz-file

用户评论:

[#1] jani at php dot net [2008-11-21 10:11:29]

This function is not binary safe. (intentionally, or not, that's the question :)

[#2] nb_nonospamm_ at wtal dot de [2005-11-27 04:12:47]

In PHP4.4.1 I noticed that gzfile only reads up to 8190 bytes per line. I had a 20K SQL query that was cut into 3 parts - and wondered why the SQL server complained. 

Reading an uncompressed file with the file() command works as expected.

[#3] The Jedi [2005-06-28 06:44:44]

A quicker way to load a gziped file in a string :
<?php
function gzfile_get_contents($filename$use_include_path 0)
{
    
//File does not exist
    
if( !@file_exists($filename) )
    {    return 
false;    }
    
    
//Read and imploding the array to produce a one line string
   
$data gzfile($filename$use_include_path);
   
$data implode($data);
   return 
$data;
}
?>

[#4] webmaster at ragnarokonline dot de [2004-01-13 11:58:30]

This works similar to gzfile() but it returns the file in a string instead of an array and doesn't write it to stdout compared to readgzfile.

Note: unlike the usual file-functions filesize won't work here, since the length-parameter of gzread refers to the uncompressed length, while filesize returns the size of the compressed file.

<?php
function gzfile_get_contents($filename$use_include_path 0) {
    
$file = @gzopen($filename'rb'$use_include_path);
    if (
$file) {
        
$data '';
        while (!
gzeof($file)) {
            
$data .= gzread($file1024);
        }
        
gzclose($file);
    }
    return 
$data;
}
?>

上一篇: 下一篇: