As per php manual I have already enable php_zip.dll in the php.ini file and also check php_zip.dll in ext folder of php installation folders. It appears there. But While using code for zip it shows errors as below:
Fatal error: Class 'ZipArchive' not found in C:\inetpub\wwwroot\projectname\bulkdownload.php on line 9
Following is the code of bulk download.php
<?php
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
$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
foreach($file_names as $files)
{
$zip->addFile($file_path.$files,$files);
}
$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");
exit;
}
$file_array = $_GET['voice'];
$file_names = explode(';', $file_array);
$archive_file_name = "voicefile.zip";
$file_path = "d:/temp_file/voice/";
zipFilesAndDownload($file_names,$archive_file_name,$file_path);
?>
Run phpinfo().
Above the "zlib" heading, see if there is a "zip" heading. If not then the zip module has not been installed correctly. If so check that it is "enabled".
Related
Hello guys im created a function to create zip file for me and download it
but when i import the file it's can't unzip my file i don't know and here is a picture
of file created by php and manualy to see the differents
here is my code:
$zip = new ZipArchive;
$zip_name = 'test.zip';
if ($zip->open($zip_name, ZipArchive::CREATE) === TRUE)
{
$zip->addFile('1.csv', 'demofile/1.csv');
$zip->addFile('2.csv', 'demofile/2.csv');
$zip->addFile('1.csv', 'demofile2/1.json');
$zip->addFromString('demofile/1.csv', $content_1);
$zip->addFromString('demofile/2.csv', $content_2);
$zip->addFromString('demofile2/1.json', $contents);
// All files are added, so close the zip file.
$zip->close();
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
header("Content-length: ".filesize($zip_name));
readfile($zip_name);
exit();
}
I am attempting to create a zip file that has a simple text file inside. Unfortunatly when I open the zip file, the file is empty, although in the code I have added in a text file.
I have looked for other questions on the topic with no avail and I have tried adding different files such as photos , but the zip is always empty.
How can I succesfully add files to my zip?
Here is the commented code
if($_POST['down']){
ob_clean(); //clean and flush
ob_end_flush();
$zip = new ZipArchive(); //create new zip
$filename = 'images.zip'; //call the zip a name
if($zip->open($filename, ZipArchive::CREATE) == FALSE){ //if zip can't be opened or created, kill the script
die('nozip');
}
$string = 'wef2emfofp32fo2mfomepofm'; // a string
$zip->addFromString('text.txt', $string); //add the string to the zip
$zip->close(); //close zip
header("Content-Disposition: attachment; filename=".basename(str_replace(' ', '_', $filename)));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/zip");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize($filename));
//download the zip
}
remove these codes
if($zip->open($filename, ZipArchive::CREATE) == FALSE){ //if zip can't be opened or created, kill the script
die('nozip');
}
and surround everything else with a try catch block. You might find answers in the exception message if that block is not itself the cause.
With this simple code (from php documentaion)
I have created succesfully the zip and the content
<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
$zip->addFromString('test.txt', 'file content goes here');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
I had a set of files in a folder. from this few will select and adding to zip. But it adding non selected files also.
The sample file names are
EXPLORER_TRM_FDM_147461_B_280.pdf,
EXPLORER_TRM_FDM_147463_B_130.txt,
EXPLORER_TRM_FDM_147470_B_130.pdf,
From this I am selecting only second one but all are coming.
my function is
public function zipFilesDownload($file_names,$archive_file_name,$file_path)
{
$zip = new ZipArchive();
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$archive_file_name>\n");
}
foreach($file_names as $files){
$zip->addFile($file_path.strtolower($files),$files);
}
$zip->close();
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Pragma: no-cache");
header("Expires: 0");
readfile("$archive_file_name");
exit;
}
$file_names contain only
EXPLORER_TRM_FDM_147463_B_130.txt
but the other two also adding into zip. where I am wrong?
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 managed to zip the files and downloaded it. The only problem is that I cant extract the zip file is says "An error occured while loading the archive". It contains data since it has around 1000kb. Is there something wrong with my code?
$zip = new ZipArchive();
$zip_name = "zipfile.zip";
if($zip->open($zip_name, ZipArchive::CREATE) !== TRUE) {
echo("Zip failed");
} else {
foreach($temp_file as $file) {
$zip->addFile($file, $file);
}
$zip->close();
header('Content-disposition: attachment; filename=media.zip');
header('Content-type: application/zip');
readfile($zip_name);
}
I've added a variable $zipname to contain the full path to your "zipfile.zip". It is later used to determine the filesize, which is send with the content-length header. Please adjust the path!
added some more headers, especially length and pragma
adjusted length and disposition to use dynamic filenames based on the variables on the top
added exit() after readfile() - rare, but you never know the registered shutdown handlers and their output behaviour
keep in mind, if you have large files you are sending through readfile() and you have a busy site, this will consume memory
Give it a try:
<?
$zip = new ZipArchive();
$zip_name = "/path/to/zipfile.zip"; // <-- adjust path
$file_name = basename($zip_name);
if($zip->open($zip_name, ZipArchive::CREATE) !== TRUE) {
echo("Zip failed");
} else {
foreach($temp_file as $file) {
$zip->addFile($file, $file);
}
$zip->close();
header("Pragma: public");
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Length: " . filesize($zip_name));
header("Content-Transfer-Encoding: binary");
readfile($zip_name);
exit;
}