i will create a Zip Archive with the PHP Class. But it still not working.
It comes no fail and the response is 1. He don't create the Zip-File.
$zip = new ZipArchive;
$res = $zip->open('qr_img/'.'ab387bas.zip'.'', ZipArchive::CREATE);
if ($res === TRUE) {
$zip->addFile($file, 'screen.png');
$zip->close();
}
know's everyone the answer ?
$file_names is the array of files which are you want to add in the zip.
function zipFile($file_names,$archive_file_name)
{
$zip = new ZipArchive();
//create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZIPARCHIVE::OVERWRITE )!==TRUE) {
}
//add each files of $file_name array to archive
foreach($file_names as $files)
{
// $zip->addFile($files,$files);
$zip->addFromString(basename($files), file_get_contents($files));
}
$zip->close();
}
Related
I'm using following codes to create zip file in Laravel:
$files = Answer::where('questionid', $questionId)
->pluck('answer')
->all();
$zip = new ZipArchive;
$zip_name = time() . "_$questionId" . ".zip";
if ($zip->open(storage_path($zip_name), ZipArchive::CREATE) === true) {
foreach ($files as $file) {
$relativeNameInZipFile = explode('/', $file)[3];
$zip->addFile(Storage::path($file), $relativeNameInZipFile);
}
$zip->close();
}
return Storage::download($zip_name);
It works properly.But when the last line of the code that returns file to download create an extra zip file in the storage!
return Storage::download($zip_name);
Why!!??
I'm trying to make a existing folder to a zip and download it using php. The existing folder contain some files. This is my code
$folder = 'excel/report/[line: '.$args_line.'][startDate: '.$start_date.'][endDate: '.$end_date.']';
$zip_name = 'excel/report/[line: '.$args_line.'][startDate: '.$start_date.'][endDate: '.$end_date.'].zip';
$zip = new ZipArchive;
if($zip -> open($zip_name, ZipArchive::CREATE ) === TRUE) {
$dir = opendir($folder);
while($file = readdir($dir)) {
if(is_file($folder.$file)) {
$zip -> addFile($folder.$file, $file);
}
}
$zip ->close();
}
But when i called the API zip folder is not created. Please go through my code and help me to solve it. Thanks in advance
// Get real path for our folder
$rootPath = realpath('folder-to-zip');
// Initialize archive object
$zip = new ZipArchive();
$zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** #var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();
Try below code.
$folder = 'excel/report/[line: '.$args_line.'][startDate: '.$start_date.'][endDate: '.$end_date.']';
$zip_name = 'excel/report/[line: '.$args_line.'][startDate: '.$start_date.'][endDate: '.$end_date.'].zip';
$zip = new ZipArchive;
$zip->open($zip_name, ZipArchive::CREATE);
foreach (glob($folder) as $file) {
$zip->addFile($file);
}
$zip->close();
i want to download file to zip using php zipArchive but i am getting this error
'N Multi-disk zip archives not supported'.
$file_names is the array of files which you want to add in the zip
function zipFile($file_names,$archive_file_name)
{
$zip = new ZipArchive();
//create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZIPARCHIVE::OVERWRITE )!==TRUE) {
}
//add each files of $file_name array to archive
foreach($file_names as $files)
{
// $zip->addFile($files,$files);
$zip->addFromString(basename($files), file_get_contents($files));
}
$zip->close();
}
I have a problem.
I searched the internet for a solution, but nothing works well. I'm trying to make a script that will take files from a folder and threw them into a zip archive.
But it doesn't work ..
I tried through different paths, but it's only me mixed up and nothing came of it.
Here is the current code that is according to me the easiest and should workbut doesn't do that ..
Can you help me?
function archivebackup(){
$zip = new ZipArchive;
if ($zip->open('Mail.zip', ZipArchive::CREATE) === TRUE){
foreach (new DirectoryIterator('Mails/') as $fileInfo) {
$fileName = $fileInfo->getFilename();
// echo $fileName ."<br>";
$zip->addFile($fileName);
}
$zip->close();
}
}
It seems you're not including files in your current working directory and getFilename() only returns a filename without a path.
Do the following:
function archivebackup(){
$zip = new ZipArchive;
if ($zip->open('Mail.zip', ZipArchive::CREATE) === TRUE){
foreach (new DirectoryIterator('Mails/') as $fileInfo) {
if (in_array($fileInfo->getFilename(), [ ".", ".." ]) { continue; }
$fileName = $fileInfo->getPathname();
$zip->addFile($fileName);
}
$zip->close();
}
}
What is the easiest way of creating dynamically zip files with php?
For example,
I have these files on the server,
Root -> Folder 1 -> file1.wav
Root -> Folder 2 -> file2.jpg
I want to create a zip file contains these 2 files and allow user to download it.
any help ?
Thx in advance
See the ZipArchive class; it has what you need.
http://www.php.net/manual/en/class.ziparchive.php
Example from PHP.Net:
<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$zip->addFile('/path/to/index.txt', 'newname.txt');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
After zipping it, you can output a mime-type header and output the file:
header('Content-type: application/zip');
readfile('test.zip');
this is the working example of making zip in php
$zip = new ZipArchive();
$zip_name = time().".zip"; // Zip name
$zip->open($zip_name, ZipArchive::CREATE);
foreach ($files as $file) {
echo $path = "uploadpdf/".$file;
if(file_exists($path)){
$zip->addFromString(basename($path), file_get_contents($path));
}
else{
echo"file does not exist";
}
}
$zip->close();