I am getting a base64 binary encoded data from an API request response. I need to save it as zip file. I use the code like below.
<?php
$zipStr = 'UEsDBBQACAAIABprdEEAAAAAAAAAAAAAAAAWAAAAb2JqZWN0cy9Db250YWN0Lm9iamVjdI2SX0vDMBTF3/cpSt9NOhERyTJCl+Gga6VNBZ9GlmWuI21mk0799ka7P50TXR4C9+R3L5ecg4bvpfK2sjaFrgZ+HwS+JyuhF0X1MvBzNr6684e4h8LGWF0m87UU1nMtlRn4K2s39xAazTfALHUtJBC6hNdBcAuDG1hKyxfcch/3PHeQKox9KuSbaetvbdkoFfNSYqEry4WdpXxdzBE86EdUaNWUlcHjPIpmMZlSBPfSOUTCMMljBv7jwiRmJGSATVh0Efj4kMS0fwlJp2QS/Q2mFOQZTTNAognJfmOXhbKyzoTeSEydSx925Yxx/9PRf9Kd/p0q1eKwVphSwuhoNnL315yvt1Pezay5dXHAShqT1PS14QrBo3yKb7lqJGbJiDwj2BbHjeDZSkjxuVR7v72d363Y5gR2goJgN3i49wlQSwcI3kTEMT8BAACvAgAAUEsDBBQACAAIABprdEEAAAAAAAAAAAAAAAALAAAAcGFja2FnZS54bWxNj00LwjAMhu/7FaN3lzpkiHTdQfDkQUS9StbFWbXtWIsf/96xDzSX5CF53ySieJtH/KTWa2dzNk84i8kqV2lb5+x42MyWrJCR2KG6Y01xN219zq4hNCsA77BJ/MW1ihLlDKScZ8AXYChghQGZjOIuRPg05Ie6Z0Om7FbKtbMBVei0fT7v8aZLAVP7J7BoSG61DydNLwE9Dtbw5y3GP2SaJVzARJGA8XwZfQFQSwcIaf0pNKsAAADwAAAAUEsBAhQAFAAIAAgAGmt0Qd5ExDE/AQAArwIAABYAAAAAAAAAAAAAAAAAAAAAAG9iamVjdHMvQ29udGFjdC5vYmplY3RQSwECFAAUAAgACAAaa3RBaf0pNKsAAADwAAAACwAAAAAAAAAAAAAAAACDAQAAcGFja2FnZS54bWxQSwUGAAAAAAIAAgB9AAAAZwIAAAAA';
header('Content-Type: application/zip');
header('Content-disposition: filename="my.zip"');
$out = base64_decode($zipFileValue);
print($out);
?>
But I am getting a SFX zip Archive as downloadable option. which I can`t able to unzip using PHP. I can unzip other normal zip file but not this SFX zip.
Can anybody help me to save the encoded data as zip or tell me how to unzip a SFX zip archive.
You need to create a temporary file for the zip archive, then use ZipArchive() to add your string to the archive as a file. Try the example below.
<?php
$zipStr = 'UEsDBBQACAAIABprdEEAAAAAAAAAAAAAAAAWAAAAb2JqZWN0cy9Db250YWN0Lm9iamVjdI2SX0vDMBTF3/cpSt9NOhERyTJCl+Gga6VNBZ9GlmWuI21mk0799ka7P50TXR4C9+R3L5ecg4bvpfK2sjaFrgZ+HwS+JyuhF0X1MvBzNr6684e4h8LGWF0m87UU1nMtlRn4K2s39xAazTfALHUtJBC6hNdBcAuDG1hKyxfcch/3PHeQKox9KuSbaetvbdkoFfNSYqEry4WdpXxdzBE86EdUaNWUlcHjPIpmMZlSBPfSOUTCMMljBv7jwiRmJGSATVh0Efj4kMS0fwlJp2QS/Q2mFOQZTTNAognJfmOXhbKyzoTeSEydSx925Yxx/9PRf9Kd/p0q1eKwVphSwuhoNnL315yvt1Pezay5dXHAShqT1PS14QrBo3yKb7lqJGbJiDwj2BbHjeDZSkjxuVR7v72d363Y5gR2goJgN3i49wlQSwcI3kTEMT8BAACvAgAAUEsDBBQACAAIABprdEEAAAAAAAAAAAAAAAALAAAAcGFja2FnZS54bWxNj00LwjAMhu/7FaN3lzpkiHTdQfDkQUS9StbFWbXtWIsf/96xDzSX5CF53ySieJtH/KTWa2dzNk84i8kqV2lb5+x42MyWrJCR2KG6Y01xN219zq4hNCsA77BJ/MW1ihLlDKScZ8AXYChghQGZjOIuRPg05Ie6Z0Om7FbKtbMBVei0fT7v8aZLAVP7J7BoSG61DydNLwE9Dtbw5y3GP2SaJVzARJGA8XwZfQFQSwcIaf0pNKsAAADwAAAAUEsBAhQAFAAIAAgAGmt0Qd5ExDE/AQAArwIAABYAAAAAAAAAAAAAAAAAAAAAAG9iamVjdHMvQ29udGFjdC5vYmplY3RQSwECFAAUAAgACAAaa3RBaf0pNKsAAADwAAAACwAAAAAAAAAAAAAAAACDAQAAcGFja2FnZS54bWxQSwUGAAAAAAIAAgB9AAAAZwIAAAAA';
// Prepare Tmp File for Zip archive
$file = tempnam("tmp", "zip");
$zip = new ZipArchive();
$zip->open($file, ZipArchive::OVERWRITE);
// Add contents
$zip->addFromString('your_file_name', base64_decode($zipStr));
// Close and send to users
$zip->close();
header('Content-Type: application/zip');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="file.zip"');
readfile($file);
unlink($file);
I tried the below way and it worked, nice and easy.
$folderPath = "/this is your folder path to save the zip file/";
$zipStr = "UEsDBBQACAAIABprd_etc";
$zip_Array = explode(";base64,", $zipStr);
$zip_contents = base64_decode($zip_Array[1]);
$file = $folderPath . uniqid() . '.zip';
file_put_contents($file, $zip_contents);
Related
i have some question to tell , how can i download zip and rename the files inside the zip with some php codes ? here is my code :
$file_path='../../files_pangkat/';
$file_names[0] = $ck_sk_terakhir[file_pangkat];
$zip = new ZipArchive();
//create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$archive_file_name>\n");
}
//add each files of $file_name array to archive
$zip->addFile($file_path.$file_names[0],$file_names[0]);
$zip->renameName($file_names[0], 'SK_PNS_'.$c_bio[nip_baru].'pdf');
echo $file_path.$file_names[0],$file_names[0]."<br />";
$zip->close();
//then send the headers to foce download the zip file
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Pragma: no-cache");
header("Expires: 0");
readfile("$archive_file_name");
unlink("$archive_file_name");
exit;
$file_names was the name of file that i saved on my database , but when i use $zip->renameName() , it doesn't work ..
It doesn't seem to make a lot of sense to first add a file to the zip file and then rename the newly added file right after that.
Instead, you should add the file with the name you want it to have, also see the manual on addFile().
So you should change:
$zip->addFile($file_path.$file_names[0],$file_names[0]);
$zip->renameName($file_names[0], 'SK_PNS_'.$c_bio[nip_baru].'pdf');
to:
$zip->addFile($file_path . $file_names[0], 'SK_PNS_' . $c_bio[nip_baru] . 'pdf');
Zip creates a lock on the file..you cant rename on the fly ,close it and rename again.
Try it.
$zip->addFile('.....');
....
$zip->close();
$zip->renameName($file_names[0], 'SK_PNS_'.$c_bio[nip_baru].'pdf');
I'm trying to download a folder as zip but I always get a zip with 0 byte files. I tried all the solutions found on the internet but it did not help. This is the code:
$files= scandir($dir);
$zip = new ZipArchive();
$tmp_file = tempnam('.','');
if ( $zip->open($tmp_file, ZipArchive::CREATE)===TRUE) {
# 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=$nome_dir.zip");
header("Content-length: " . filesize($tmp_file));
header('Content-type: application/zip');
readfile($tmp_file);
}
I can not understand where I'm wrong
This may be related to Memory Allocation issues in PHP, e.g.
My PHP zip function is using too much memory
Also check this: https://www.airpair.com/php/fatal-error-allowed-memory-size
I have a question
I have looked all over stacked overflow, on how to make a zip out of multiple files, and I got this code:
<?php
$data = $_POST['data'];
$zip = new ZipArchive();
$zip_name = dirname(__FILE__)."/billeder.zip"; // Zip name
$zip->open($zip_name, ZipArchive::CREATE);
foreach ($data as $file) {
$path = dirname(__FILE__)."/".$file;
$zip->addFromString(basename($path), file_get_contents($path));
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.basename($zip_name));
header('Content-Length: ' . filesize($zip_name));
ob_clean();
flush();
readfile($zip_name);
unlink($zip_name);
?>
the post data look something like this:
["Billede_008.JPG","Billede_011.JPG"]
a list of images.
My problem is that, I don't receive the zip file, when posting to the url.
I have checked that it got the post data, I've checked that all files in the post exists, and I have checked that the files were added to the zip.
here is the response from chrome. as you can see there's nothing wrong with the response, but I still don't get the zip.
Can anyone see, what I'm doing wrong?
I have been using a PHP script for a while to download .jpg files from current directory. I was wondering if it is possible to download .jpgs from current and all sub-directories, prompt for download and delete the archive after download is finished?
Here is my script (Currently this script does not delete archive after download):
<?php
$zip = new ZipArchive;
$download = 'FileName.zip';
$zip->open($download, ZipArchive::CREATE);
foreach (glob("*.jpg") as $file) { /* Add appropriate path to read content of zip */
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename = $download");
header('Content-Length: ' . filesize($download));
header("Location: $download");
?>
I have an array of links to mp3 files. I need to have all of the mp3s downloaded from a server and then zipped.
This code below is not working:
Any suggestions?
$audio_flies = array('http://example.com/1.mp3','http://example.com/2.mp3');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($audio_files as $file) {
echo "$file";
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
The archive is either in unknown format or damaged
You first need to download those files and store them locally on your server. Once that is done you will be able to zip them and make the zip file available for download.
The most basic way of achieving this is by using PHP's file_get_contents(), file_put_contents() and parse_url().
This code has not been thoroughly tested.
// Fixed the name of the array
$audio_files = array('http://example.com/1.mp3', 'http://example.com/2.mp3');
// You need to change this to
// a path where you want to store your
// audio files (Absolute path is best)
$local_path = '/local/path/here/';
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
// Download the files
// Not an elegant way to
// download files
foreach ($audio_files as $file) {
// First let's get the file name
// by parsing the URL
// and trimming the '/'
$file_name = ltrim(parse_url($file, PHP_URL_PATH), '\/');
// Download the file
$a_contents = file_get_contents($file);
// Place the downloaded content
// in the defined local path
file_put_contents($local_path . $file_name, $a_contents);
// Add the file to archive
$zip->addFile($local_path . $file_name);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
This is a very bad way to achieve your task for the following reasons (among many others):
There is no timeout for the download process
If a file fails to download it will halt the execution
It may take a very long time (depending on the size of the audio files) to serve the zip file
There is no proper error / exception handler
Many other reasons regarding structure that I'm not gonna go through here.
try the following: use parse_url to get the actual filename, use file_get_contents and file_put_contents to download and save the data locally...
$audio_flies = array('http://example.com/1.mp3','http://example.com/2.mp3');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($audio_files as $file) {
echo "$file";
$parsed = parse_url($file);
$localfile = "." . $parsed["path"];
$mp3 = get_file_contents($file);
file_file_contents($localfile, $mp3);
$zip->addFile($localfile);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);