Can't unzip file after download php - php

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();
}

Related

ZipArchive downloading empty zip files

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';
}
?>

compression problems excell using Maatwebsite

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");

renaming files inside zip while download on php

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');

PHP- ZipArchive 0 byte files

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

Zip directory and sub directories recursively specific file format and download

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");
?>

Categories