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';
}
?>
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();
}
the problem very easy the form that i try to compress a file is the next
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
$zip->addFile(Excel::download(new SheetsExports($page1, $page2), $filename . '.xls'));
$zip->close()
echo 'Archive created!'
header('Content-disposition: attachment; filename=files.zip');
header('Content-type: application/zip');
<!-- end snippet -->
$zip->close()
echo 'Archive created!';
header('Content-disposition: attachment; filename=files.zip');
header('Content-type: application/zip');
in a laravel controller , i use the external library Maatwebsite
then when i execute the brwoser apair the next error
Corrupted Content Error
The site at http://hall.test/indiceDeRotacion?_token=2Eh6WAl0W43l8ANzLz0e4GGW9reV04ESpURvEi1H&almacen=PRINCIPAL&fechaDesde=2018-05-01&fechaHasta=2019-04-30&proveedor=3000 familia=&type=xls&compresion=on&email=&asunto=&submit=informe
so i need a solution , or another external library or the correct way in my code for it works
i fix the issue with this code
$zip = new ZipArchive;
if ($zip->open('yyyy.zip', ZipArchive::CREATE) === true) {
$zip->addFile(Excel::download(new SheetsExports($page1, $page2), $filename . '.xls')->getFile(),
'xxxx.xls');
$zip->close();
}
return response()->download("yyyy.zip");
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 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 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;
}