Domanda

This is a code i found over internet, intended to create a .zip of the entire folder and download it.

I want to do a few changes, but everything I tried doesn't work. What I want:

  • Make it generic (i.e. I want to have that makezip.php in root folder. I've a file manager, and everytime I call this script from some location (ex. www.domain.com/files/media/images/ it gets that folder).

  • Delete the zip after the download (I think I did it well, using the comand unlinkbut I'm not sure that's the right way.

  • Remove that . and .. folder that the zip gets too.

  • Export the zip with the name date.hour.directory.zip (ex 18Sep2013_13-26-02_images.zip). I tried to do this $fd = getcwd(); $filename = date("dMY_H:i:s").'_'.$fd.'.zip'; but it didn't worked, it only gets the $fd variable.

I know it isn't supposed to give you all the code and expect that you will look on it nor debug it, but I've tried everything and nothing of it works. I'm learning by myself and I'm a bit newbie on php.

Best regards

<?php
        function download($file) {
                header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename='.basename($file));
                header('Content-Transfer-Encoding: binary');
                header('Expires: 0');
                header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                header('Pragma: public');
                header('Content-Length: ' . filesize($file));
                ob_clean();
                flush();
                readfile($file);
        unlink($file);
                exit;
        }

        $directory = '.';
        $files = scandir($directory);
        if(empty($files)) 
        {
                echo("You haven't selected any file to download.");
        } 
        else 
        {
                $zip = new ZipArchive();
                $filename =  date("dMY_H:i:s").'_arquivo.zip'; //adds timestamp to zip archive so every file has unique filename
                if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { // creates new zip archive
                                exit("Cannot open <$filename>\n");
                }
                $N = count($files);
                for($i=0; $i < $N; $i++)
                {
                  $zip->addFile($files[$i], $files[$i]); //add files to archive
                }
                $numFiles = $zip->numFiles;
                $zip->close();

                $time = 8; //how long in seconds do we wait for files to be archived.
                $found = false;
                for($i=0; $i<$time; $i++){

                 if($numFiles == $N){   // check if number of files in zip archive equals number of checked files
                        download($filename);
                        $found = true;
                        break;
                 }
                 sleep(1); // if not found wait one second before continue looping
         }

         if($found) { }
                 else echo "Sorry, this is taking too long";
         }
?>
È stato utile?

Soluzione

Ok, I've figured out my problems by myself, and I'm sharing the solution for those who want.

  • Make it generic (i.e. I want to have that makezip.php in root folder. I've a file manager, and everytime I call this script from some location (ex. www.domain.com/files/media/images/ it gets that folder).

I modified the code on index.php, and made a few changes on zip.php (the file which is handling the zipping process. Simples solution.. Dumb!

1 - index.php - just put a simple link

<a href="zip.php?dirz=HERE_GOES_THE_FOLDER_URL">Download folder as a .zip</a>

2 - zip.php

$directory = $_REQUEST['dirz'];

  • Delete the zip after the download (I think I did it well, using the comand unlinkbut I'm not sure that's the right way.

Yes, it's correct. (on zip.php)

unlink($ZIPNAME);


  • Remove that . and .. folder that the zip gets too.

$direct = array_diff(scandir($directory), array(".","..","error_log"));

foreach($direct as $file){
        if(is_file($directory.'/'.$file)){
            $zip->addFile($directory.'/'.$file, $file);
        }
}

  • Export the zip with the name date.hour.directory.zip (ex 18Sep2013_13-26-02_images.zip). I tried to do this $fd = getcwd(); $filename = date("dMY_H:i:s").'_'.$fd.'.zip'; but it didn't worked, it only gets the $fd variable.

Simple as:

$zipname = date("dMY_H-i-s").'_'.basename($directory).'.zip';

Best regards

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top