Zip and upload files using php

I am trying to pin and upload files from a folder named "upload". The zip file is loading, but I cannot open it (extract it). I get the error "Archive in unknown format or damaged."

I found the following code for the zip folder.

<?php $files = "upload/".array('Dear GP.docx','ecommerce.doc'); $zipname = 'filename.zip'; $zip = new ZipArchive; $zip->open($zipname, ZipArchive::CREATE); foreach ($files as $file) { $zip->addFile($file); } $zip->close(); header('Content-Type: application/zip'); header('Content-disposition: attachment; filename=filename.zip'); header('Content-Length: ' . filesize($zipfilename)); readfile($zipname); ?> 
+6
source share
6 answers

Thank you for your responses.

 <?php $files = array('Dear GP.docx','ecommerce.doc'); # create new zip opbject $zip = new ZipArchive(); # create a temp file & open it $tmp_file = tempnam('.',''); $zip->open($tmp_file, ZipArchive::CREATE); # loop through each file foreach($files as $file){ # download file $download_file = file_get_contents($file); #add it to the zip $zip->addFromString(basename($file),$download_file); } # close zip $zip->close(); # send the file to the browser as a download header('Content-disposition: attachment; filename=Resumes.zip'); header('Content-type: application/zip'); readfile($tmp_file); ?> 
+20
source

I spent more than 6 hours downloading zip files on my mac (localhost). Although the file was loading, I could not unzip them (getting the cpgz files). Apparently, none of the solutions mentioned in the stack overflow (around a lot of questions on loading a zip file) worked. Finally, after trial and error, I found that the following code works:

None of the people who answered earlier spoke about ob_start () and where exactly you should put it. Anyway, this was not the answer - you also need to use these three lines above ob_start ().

 $file=$zippath.$filename; if (headers_sent()) { echo 'HTTP header already sent'; } else { if (!is_file($file)) { header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found'); echo 'File not found'; } else if (!is_readable($file)) { header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden'); echo 'File not readable'; } else { while (ob_get_level()) { ob_end_clean(); } ob_start(); header($_SERVER['SERVER_PROTOCOL'].' 200 OK'); header("Content-Type: application/zip"); header("Content-Transfer-Encoding: Binary"); header("Content-Length: ".filesize($file)); header('Pragma: no-cache'); header("Content-Disposition: attachment; filename=\"".basename($file)."\""); ob_flush(); ob_clean(); readfile($file); exit; } } 
+2
source

Please use below code instead of $ zip-> addFile ($ file);

 <?php $zip->addFile($file_path, $filename); ?> 
0
source
 $files = "upload/".array('Dear GP.docx','ecommerce.doc'); 

Must be:

 $files = array('upload/Dear GP.docx','upload/ecommerce.doc'); 

If you still have problems, you should check to see if you have write permissions to the current directory. You can check the return value of close() to see if the file was actually written.

 $res = $zip->close(); var_dump($res); 

The output should be bool(true) if recording is successful.

It looks like you have the wrong var name in the filesize() call:

 header('Content-Length: ' . filesize($zipfilename)); 

Must be:

 header('Content-Length: ' . filesize($zipname)); 

You can also add an additional check to check if the files you add to the zip exist and check if the mail file exists before sending.

Edit: enable display errors to help you debug locally:

 ini_set('display_errors', 1); error_reporting(E_ALL); 
0
source

The answer is good, but add these lines before the heading. Zip file is open in all zip programs (winzip, winrar, etc.).

 if (headers_sent()) { // HTTP header has already been sent return false; } // clean buffer(s) while (ob_get_level() > 0) { ob_end_clean(); } 
0
source
  $zip = new ZipArchive; if ($zip->open('assets/myzip.zip', ZipArchive::CREATE)) { $zip->addFile('folder/bootstrap.js', 'bootstrap.js'); $zip->addFile('folder/bootstrap.min.js', 'bootstrap.min.js'); $zip->close(); echo 'Archive created!'; header('Content-disposition: attachment; filename=files.zip'); header('Content-type: application/zip'); readfile($tmp_file); } else { echo 'Failed!'; } 

I got this code to download files from a folder.

0
source

Source: https://habr.com/ru/post/958698/


All Articles