Search code examples
phpziphttp-compression

Compress the files while downloading by client


I try to develop something like dropbox(very basic one). For one file to download, it's really easy. what i want is: when client asks me big file, i zip file in server side then send to user. But if file is too big it takes too many times to zip them and send to user.

is there any way to send files while they are compressing?

thanks for your help. Here is my simple sample code

<?php

function createzip($files, $zip_file) {
$zip = new ZipArchive;
if($zip->open($zip_file, ZipArchive::CREATE) === TRUE) {
foreach($files as $file){
$zip->addFile($file);
}
$zip->close();
return true;
 }
else return false;
 }
$ss=array("jj.mp4");
createzip($ss,'archiv.zip');
if(filesize("archiv.zip")>3000){
$vv=array("archiv.zip");
createzip($vv,"bbb.zip");
}

 ?>

use do while loop in if statement. Until file size become a desired value. I don't understand how to use do while loop and does it make it that type of compresses. Please help me out.


Solution

  • I'm using this code for database backup with zipped type and download automaticly. you can get referance this codes

    <?php
    $DBUSER="username";
    $DBPASSWD="password";
    $DATABASE="dbname";
    $filename = "backup-" . date("d-m-Y") . ".sql.gz";
    $mime = "application/x-gzip";
    
    header( "Content-Type: " . $mime );
    header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
    
    $cmd = "mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best";
    passthru( $cmd );
    echo ('Backup Ok !');
    exit(0);
    ?>