Download the file on the server when generating zip with zipArchive

0

I am generating a .zip file in php with zipArchive and I have a problem and even though the file is automatically downloaded to the PC, it is also stored internally in the server. Surely it is silly but I do not give with the key, to see if you can help me. I leave the zip generation code. Thank you very much

if ($ num_docs! = 0) {

    $archivoZip = $zip->open( $zipname, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE );
    $zip->open($zipname, ZIPARCHIVE::CREATE );
    $zip->addEmptyDir($exp['num_exp']);
    do
    {           
        if($archivoZip==true){
            $zip->addFile("documentacion/" . $exp['num_exp']."/" . $doc['fichero'], $exp['num_exp']."/" . $doc['fichero']);     
        }
    }while($doc = mysql_fetch_assoc($docs));
    $zip->close();
}

///DESCARGANDO EL ZIP
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
readfile($zipname);
    
asked by Lis86 31.05.2018 в 14:43
source

1 answer

0

I have attached a function that I use every day and in which I indicate how to delete the file that is generated on your server

public function download_zip($route){
        if (file_exists($route)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.basename($route));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($route));
            ob_clean();
            flush();
            readfile($route);
            unlink($route);//Esta línea destruye el elemento zip que se genera
            exit;
        }

    }
    
answered by 31.05.2018 в 17:25