php ZipArchive can't open zip file that gets downloaded from a server.
I have a zip file that I created with winrar and uploaded to my server, file location is here: http://myserver.com/uploads/test.zip It's a valid zip file
Here is my code
When I run this code all I get is
error: Not a zip archive.
It opens the file and extracts it but complains that file already exists even if I delete file.
the file that I get when I access http://myserver.com/update/test.zip get corrupted and I have no idea why, this same code that I pasted above worked just a week ago.
The problem is actually the .zip file. I downloaded it and I cannot open the file with WinZip. Please try to make a new archive and upload it again on your server.
The code looks okay so I think it just the file that is the problem.
UPDATE #1:
The .zip file is now correct. Try to delete the files before you download and extract the new files. You can use unlink("uploads/update.zip") maybe you also need to clear the uploads/temp directory first.
UPDATE #2:
The download worked now for me. Try to add this header:
header("Content-Transfer-Encoding: Binary");
Also don't forget to close the ZipArchive after extracting:
$zip->close();
Related
I'm using Laravel 8
I've uploaded a zip file then want to download it. But here is a problem. After file download, it's not extracting.
Here is my code for download zip file:
$product = Product::where('id',1)->first();
$file = $product->main_file;
return response()->download($file,'filename.zip');
But, if I manually copy this zip file from my project then extract it, it's extracting fine.
Or, if I doing this: return Redirect()->to($file); It also downloads the file and extracts well without any problem. But, it's not a proper way to download. Isn't it?
So, What can I do now?
if you're trying to download via a link try this:
Download
but file name should be saved somewhere.
I have gotten as far as downloading a file from an external folder into a directory in my website.
The code;
<?php
$txt = file_get_contents('https://docs.google.com/document/d/1IPNaCKwzdWRVOq30saI_pIBLcL62g4_2zMmjK54yD3E/edit?usp=sharing');
file_put_contents('import.txt', $txt);
?>
I have the cron file in a folder in the same directory
https://example.com/wp-content/themes/theme-child/test.php
The code runs and a file is downloaded into the same directory with name import.txt
But the challenge is that it appears the page is downloaded as an HTML document and contains lots of CSS codes which don't exist in the file that is being downloaded.
You can check how the file I'm downloading looks like here:
https://docs.google.com/document/d/1IPNaCKwzdWRVOq30saI_pIBLcL62g4_2zMmjK54yD3E/edit?usp=sharing
So basically the downloaded file doesn't contain the target file contents. All I'm looking for is to download the file as it is without any additions.
Thanks
I have created php script to allow user to download zip files.
Script looks like:
$filePath = '/path/to/zipfile.zip'; // file is below /public_html/ directory
if(file_exists($filePath)) {
$fileName = basename($filePath);
$fileSize = filesize($filePath); //returns: 26494938
header("Content-Type: application/zip");
header("Content-Length: ".$fileSize);
header("Content-Disposition: attachment; filename=".$fileName);
readfile($filePath);
die;
};
I would expect this script to pass zipfile.zip for downloading. And it does, the save dialog pops up, I am choosing where to save, pressing save and zip file saves. Except one important thing - the file is with 0 bytes, it is completely empty zip file. Original zip file is full with files (around 25Mb). Maybe the size is the problem?
Can someone please help with some advice? Path to file is correct. I suspect that the problem is most likely related to readfile() function and/or zip file size, if so is there some alternative to readfile() that would work? Thank you!
Update: I have tried to use function readfile_chunked as mentioned in this post: "Readfile reads 0 bytes from large file?" - the result is that zip after downloading now have some size (no longer 0 bytes) and all correct content inside it, looks like everything is ok, but the zip is invalid, and cannot be unzipped and opened normally. In Win8 when trying to open downloaded zip file I am receiving this error: The Compresed (zipped) Folder zipfile.zip is invalid.. Of course the original zip file works fine. WinRAR shows this error: zipfile.zip: Unexpected end of archive. It looks the same problem as described in this post, without any working solution.
Try to add ob_end_flush(); before readfile($filePath); . It
The simplest solution is to do away with the php completely, just provide an HTML link to the zip and let your server do the work
also if you really must stick with php to serve this, you have no code there that actually sends the file to the user, readfile doesn't do that it just loads it into the program
I have a remote ZIP file, and I need to read the content of a file located in the zip file, without having to copy the zip file to our server. There should be some solution to solve this, but I can't find it.
Example:
URL - http://example.com/feed.zip and
File in the ZIP Archive is feed.xml
I need to read the content of feed.xml and save it in one variable. Thats it.
I have try with $z = new ZipArchive(); and open, but open can only read files from the server and not remote files.
A super lazy solution would be:
$feed = `wget http://example.com/feed.zip -qO- | unzip - -p feed.xml`;
Note that the url should be escaped (escapeshellarg) if it's not a fixed string.
But you could also investigate other ZIP classes than the built-in ZipArchive. PEAR or PclZip might allow to read from streams without workarounds. (But php://memory or a string stream wrapper might be feasible as well, if you're really bent on eschewing temporary files.)
I had the same problem and the best I could get was saving zip content to temp file and using ZipArchive to get content of zipped file:
public static function unzip($zipData) {
// save content into temp file
$tempFile = tempnam(sys_get_temp_dir(), 'feed');
file_put_contents($tempFile, $zipData);
// unzip content of first file from archive
$zip = new ZipArchive();
$zip->open($tempFile);
$data = $zip->getFromIndex(0);
// cleanup temp file
$zip->close();
unlink($tempFile);
return $data;
}
This is basically implementation of #mauris suggestion.
That requires the concept of temporary files. Technically speaking: yes, you are going to make a temporary copy of the remote zip file that is locally available because your ZipArchive does not work on remote zip files at all.
Following these steps to implement temporary copy should help you:
Make a copy of the remote zip file to a temporary folder.
Use ZipArchive to parse the local copy of the zip file.
Read information in your feed.xml file.
Close and everything and delete the local copy of the zip file.
In a single script, I need to do the following:
create a zip out of files in a directory
force the download of the newly create zip
My problem is that whenever I try to do this in a single script,
the downloaded zip is corrupted (filesize is ok though). If I trig the
processes in two separate script calls, the downloaded zip is ok.
I guess that the problem is that the zip saving to file process isn't completely
finished before the download starts. Strangely, it doesn't solve the problem to
insert a sleep(3) between the processes... Code outline below.
How to assure that the zip file is completely finished before the force download starts?
Regards / Jonas
// 1. create a zip
$createZipFile = new CreateZipFile('temp.zip');
$createZipFile->zipDirectory('temp/', '.');
$createZipFile->saveZipFile();
sleep(3); // <-- Doesn't matter!
// 2. force zip download
$fileServer = new FileServer();
// Line below gives a corrupted zip when run in same script as 1.
$fileServer->forzeDownload('temp.zip');
Create the zip and then redirect the user to that file.
http://php.net/manual/en/function.header.php
Thank you, JorenB and Gumbo!
The texteditor examination revealed some debug output in zip creation that doesn't affect the orignial zip, but corrupts the data sent to the browser when downloading.