简体   繁体   中英

How to download a folder with all its folders and files without zipping it with php

1 - I have a folder has a lot of users folder inside it, and every user's folder has others folders inside it has ismages inside them like this:

  • localhost/cm/app/model/uploads/mark/3/small/car.jpg
  • localhost/cm/app/model/uploads/mark/3/big/car.jpg
  • localhost/cm/app/model/uploads/stiven/9/small/pc.jpg
  • localhost/cm/app/model/uploads/stiven/9/big/pc.jpg

2 - I want that i donwload this folders with all its content.

3 - I prefer that I download it without zipping it with the same name, if it's not possible with zipping it with any name.

4 - I've used this code and it's not working because when I try to open the zip folder after downloading it an error message appear and tell me that "the archive is either unknown format or damaged", and this my code:

<?php
$dir = 'http://localhost/cm/app/model/uploads'; //folder path

$archive = time().'download.zip';

$zip = new ZipArchive;
$zip->open($archive, ZipArchive::CREATE);
$files = scandir($dir);
unset($files[0], $files[1]);
foreach ($files as $file) {
$zip->addFile($dir.'/'.$file);
}
$zip->close();

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$archive);
header('Content-Length: '.filesize($archive));
readfile($archive);
unlink($archive);
?>

*So how to download my folder with all its folders and images without zipping it or with zipping if not possible?

I suspect you've got an error in you're code. maybe ext-zip isn't install one way of checking this is by running php -m in a terminal which will display all install and enabled modules.

If you open up the zip file with a text editor it's like there be an error message instead of archived data.

This is The best way that i found:

 <?php
 // Get real path for our folder
 $rootPath = realpath('folder_for_ziping');

 // Initialize archive object
 $zip = new ZipArchive();
 $zip->open('file.zip', 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();
$archive = "file.zip";
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$archive);
header('Content-Length: '.filesize($archive));
readfile($archive);
unlink($archive);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM