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");
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 am trying to download all files in zip folder on button click but my code isn't working. here following is my code
getting data into database
put files in array
i already have zip folder named "office_specs.zip"
but something is wrong
public function downloadSpecs(){
$officeSpecs = DB::table('specifications_draged_files')->where('user_id', Auth::user()->id)->get();
$files = array();
foreach($officeSpecs as $officeSpec){
$files[] = 'http://abc.def.biz/uploads/zeda_specs/'.Auth::user()->id.'/files/'.$officeSpec->draged_file;
}
$zipname = 'http://abc.def.biz/uploads/zeda_specs/office_specs.zip';
$zip = new \ZipArchive();
$zip->open($zipname, \ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
}
Following is the error i am facing
This site can’t be reached The webpage at http://abc.def.biz/download-specs might be temporarily down or it may have moved permanently to a new web address.
ERR_INVALID_RESPONSE
I use this way for download multiple files as zip :
$files = array('readme.txt', 'test.html', 'image.gif');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
But the file file.zip be save on my host . I don't want to save on my host i just wanna download it withou save .
Thank
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);