简体   繁体   中英

Download Zip file in php

I want to download my zip file by a variable ($direccion) that I want to assign to it and when I try to do it, it comes out that the file is corrupted.

$file_name = basename('C:/xampp/htdocs/issv/upload/26908557.zip');
 
   header("Content-Type: application/Zip");
   header("Content-Disposition: attachment; filename=26908557.zip");
   header("Content-Length: " . filesize('C:/xampp/htdocs/issv/upload/26908557.zip'));

  readfile('C:/xampp/htdocs/issv/upload/26908557.zip');
   exit;

that's my code and it only works that way, but I want to put the $direccion path to it

$direccion='c:/xampp/htdocs/issv/upload/'.trim($cedula);

this is what my variable $cédula means $cedula=$_POST['cedula'];

There many ways of zip file creation which are;

$zip_file = '(path)/filename.zip';$dir = plugin_dir_path( __FILE__ );
$zip_file = $dir . '/filename.zip';$zip = new ZipArchive();
if ( $zip->open($zip_file, ZipArchive::CREATE) !== TRUE) {
exit("message");
} $zip->addFile('full_path_of_the_file', 'custom_file_name); $download_file = file_get_contents( $file_url );
$zip->addFromString(basename($file_url),$download_file); $zip->close();

Or simply do this:

$url = "http://anysite.com/file.zip";$zip_file = "folder/downloadfile.zip";$zip_resource = fopen($zipFile, "w");$ch_start = curl_init();curl_setopt($ch_start, CURLOPT_URL, $url);curl_setopt($ch_start,CURLOPT_FAILONERROR, true);curl_setopt($ch_start,CURLOPT_HEADER, 0);curl_setopt($ch_start,CURLOPT_FOLLOWLOCATION, true);curl_setopt($ch_start,CURLOPT_AUTOREFERER, true);curl_setopt($ch_start,CURLOPT_BINARYTRANSFER,true);curl_setopt($ch_start,CURLOPT_TIMEOUT, 10);curl_setopt($ch_start,CURLOPT_SSL_VERIFYHOST, 0);curl_setopt($ch_start,CURLOPT_SSL_VERIFYPEER, 0);curl_setopt($ch_start,CURLOPT_FILE,$zip_resource);$page =curl_exec($ch_start);if(!$page){echo "Error :- ".curl_error($ch_start);}curl_close($ch_start);$zip = new ZipArchive;$extractPath = "Download File Path";if($zip->open($zipFile) != "true"){echo "Error :- Unable to open the Zip File";}$zip->extractTo($extractPath);$zip->close();
class FileNotFound extends RuntimeException {}

$downloadUploadedZip = function(string $filename): void {
    $directory = 'C:/xampp/htdocs/issv/upload';

    if (dirname($filename) !== '.') {
        $directory = dirname($filename);
    }

    $filename = basename($filename);
    $filepath = sprintf('%s/%s', $directory, $filename);

    if (file_exists($filepath)) {
        header("Content-Type: application/zip");
        header(sprintf("Content-Disposition: attachment; filename=%s", $filename));
        header(sprintf("Content-Length: %d", filesize($filepath)));

        readfile($filepath);

        exit;
    }

    throw new FileNotFound(sprintf('File %s not found.', $filepath));
};

$downloadUploadedZip('26908557.zip');
$downloadUploadedZip('C:/xampp/htdocs/issv/upload/26908557.zip');

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