Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to zip a whole directory and download using php [duplicate]

I am self studying php and I am creating a sample test site which lets the user input the file code which will be used to determine the file path of the folder to be downloaded. The code i have below will only download a single file. What i want now is to download and zip the whole directory. Please help. Thank you in advance

    <h3>Search Client File</h3>
            <form  method="post" action="#"  id="searchform">
              Type the Image Code:<br><br>
                  <input  type="text" name="icode">
            <br>
      <input  type="submit" name="submit" value="Search">
            </form>  

<?php
     $fcode=$_POST["icode"];
 if (!empty($fcode))
   {

$file="/var/www/website/$fcode.tif";

     if (file_exists($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');
       header('Pragma: public');
       header('Content-Length: ' . filesize($file));
       ob_clean();
       ob_end_flush();
       readfile($file);

      }

      else
      {
        echo "The file $fcode.tif does not exist";
      } 
   }    

     else
     {
       echo "No Values";
     }

    ?>
like image 938
Xander Vane Avatar asked Apr 26 '15 03:04

Xander Vane


People also ask

How can I download multiple zip files in PHP?

For implementing the process of creating zip file with the uploaded files I have used the PHP ZipArchive class. This class is loaded in the PHP code and loaded with the uploaded file binaries. In this example, the user can choose and upload multiple files via a HTML form.

How zip all files in folder in PHP?

Place the php files along with the directory to be zipped in C:\xampp\htdocs(XAMPP is installed in C: drive in this case). In the browser, enter https://localhost/zip.php as the url and the file will be zipped. After this a new zip file is created named 'file'.

How do I zip an entire folder?

To zip (compress) a file or folder Locate the file or folder that you want to zip. Press and hold (or right-click) the file or folder, select (or point to) Send to, and then select Compressed (zipped) folder. A new zipped folder with the same name is created in the same location.

How do I download an entire zip file?

Right-click the file you want to zip, and then select Send to > Compressed (zipped) folder. Open File Explorer and find the zipped folder. To unzip the entire folder, right-click to select Extract All, and then follow the instructions. To unzip a single file or folder, double-click the zipped folder to open it.


1 Answers

<?php

$dir = 'dir';
$zip_file = 'file.zip';

// Get real path for our folder
$rootPath = realpath($dir);

// Initialize archive object
$zip = new ZipArchive();
$zip->open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE);

// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($rootPath),
    RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($files as $name => $file)
{
    // Skip directories (they would be added automatically)
    if (!$file->isDir())
    {
        // Get real and relative path for current file
        $filePath = $file->getRealPath();
        $relativePath = substr($filePath, strlen($rootPath) + 1);

        // Add current file to archive
        $zip->addFile($filePath, $relativePath);
    }
}

// Zip archive will be created only after closing object
$zip->close();


header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($zip_file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($zip_file));
readfile($zip_file);

?>

Read more at:

How to zip a whole folder using PHP

like image 180
Adrian Cid Almaguer Avatar answered Oct 08 '22 17:10

Adrian Cid Almaguer