gzfile

(PHP 4, PHP 5, PHP 7, PHP 8)

gzfileLiest eine ganze gz-Datei in ein Array

Beschreibung

gzfile(string $filename, int $use_include_path = 0): array|false

Diese Funktion entspricht weitgehend readgzfile(), gibt die Datei aber in einem Array zurück.

Parameter-Liste

filename

Der Dateiname.

use_include_path

Sie können diesen optionalen Parameter auf 1 setzen, wenn Sie die Datei auch im include_path suchen lassen wollen.

Rückgabewerte

Ein Array, das die Zeilen der Datei als Elemente enthält. Leere Zeilen sind enthalten, und Zeilenumbrüche sind noch angefügt. Bei einem Fehler wird false zurückgegeben..

Beispiele

Beispiel #1 gzfile()-Beispiel

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

Siehe auch

add a note add a note

User Contributed Notes 4 notes

up
3
nb_nonospamm_ at wtal dot de
18 years ago
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.
up
3
The Jedi
18 years ago
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;
}
?>
up
1
jani at php dot net
15 years ago
This function is not binary safe. (intentionally, or not, that's the question :)
up
0
webmaster at ragnarokonline dot de
20 years ago
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($file, 1024);
        }
       
gzclose($file);
    }
    return
$data;
}
?>
To Top