略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: gzgets

2024-12-20

gzgets

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

gzgetsGet line from file pointer

说明

gzgets(resource $stream, ?int $length = null): string|false

Gets a (uncompressed) string of up to length - 1 bytes read from the given file pointer. Reading ends when length - 1 bytes have been read, on a newline, or on EOF (whichever comes first).

参数

stream

The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen().

length

The length of data to get.

返回值

The uncompressed string, or false on error.

更新日志

版本 说明
8.0.0 length is nullable now; previously, the default was 1024.

范例

示例 #1 gzgets() example

<?php
$handle 
gzopen('somefile.gz''r');
while (!
gzeof($handle)) {
   
$buffer gzgets($handle4096);
   echo 
$buffer;
}
gzclose($handle);
?>

参见

add a noteadd a note

User Contributed Notes 3 notes

up
1
prismngp1 at yahoo dot com
19 years ago
<?
// this is simple code by VIJAY to unzip .gz file
$file = "/absolute/path/to/your/file" ;
$fp = fopen("$file", "w") ;
// file to be unzipped on your server
$filename = "filename.gz" ;
$zp = gzopen($filename, "r");

if ($zp)
{
  while (!gzeof($zp))
  {
    $buff1 = gzgets ($zp, 4096) ;
    fputs($fp, $buff1) ;
  }               
}           
gzclose($zp) ;
fclose($fp) ;
?>
up
1
divinity76 at gmail dot com
2 years ago
PS when it encounters and breaks on a newline byte ("\n"), the newline byte itself is not included in the returned string.
up
0
Anonymous
16 years ago
For the above example by VIJAY, using gzgetc would be better, as I've encountered binary/text file incompatibilities (at least with PHP 4.0.4).

官方地址:https://www.php.net/manual/en/function.gzgets.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3