Home » Php » gzread() fails when continue reads in seperate php requests

gzread() fails when continue reads in seperate php requests

Posted by: admin October 26, 2017 Leave a comment


I have gz compressed file and uncompressing it to normal file so I use fwrite(). It works fine when do uncompress in a single PHP request.

Because of large compressed files, I do mind in PHP timeouts, I uncompress the file until 30 seconds and stop the process and store the current offset of gz file using gztell() then continue it form the next PHP request where I left.

I do gzseek() with current offset and continue the uncompression but gzread() continuously gives empty string

function gz_uncompress_file($source, $offset = 0){

    $dest =  str_replace('.gz', '', $source);

    $fp_in = gzopen($source, 'rb');

    if (empty($fp_in)) {
        return 'Cannot open gzfile to uncompress sql';

    $fp_out = fopen($dest, 'ab');

    if (empty($fp_out)) {
        return 'Cannot open temp file to uncompress sql';

    gzseek($fp_in, $offset);

    $break = false;

    while (!gzeof($fp_in)){

        $chunk_data = gzread($fp_in, 1024 * 512);

        if (empty($chunk_data)) {
            return 'empty string so stop the process';

        fwrite($fp_out, $chunk_data);

        //Clearning to save memory


        $break = true;
        $offset = gztell($fp_in);


    if ($break) {
        //Saving offset
        $this->set_option('sql_gz_uncompression_offset', $offset);

        return 'continue_from_call';

    echo "Un compression done";