I want create a zip in a specific Directory. Actually i can create a folder with this code:
function createZip($array)
{
$files = $array;
$zipname = 'ftpack.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
print_r($array);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
}
But this code create a zip in the principal directory, i want create in the at /zipDownload directory.
Just pass the full path:
$zipname = '/path/to/ftpack.zip';
Also make sure that the folder /path/to is writable by PHP.
Related
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 need to create a Zip file in a specific file path without any sub folders created inside it,
here is my code :
$files = array('article/includes/pdfFiles/file1.txt','article/includes/pdfFiles/file2.txt');
$zipname = 'article/includes/pdfFiles/file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
I need the file.zip to be created in the path 'assets/uploads/pdfFiles' in this folder, its been created but the problem is inside the zip file these folders exits: assets/uploads/pdfFiles
How can I create the zip file without these folders?
Your code above doesn't work because you don't show the contents of $fileNames but I am assuming it is something like:
array('assets/uploads/pdfFiles/file1.txt','assets/uploads/pdfFiles/file2.txt',...)
When you add to the zip do this:
foreach ($fileNames as $fullpath) {
// pull just the file name from the path
$filename = substr($fullpath, strrpos($fullpath, '/')+1);
$zip->addFile($fullpath,$filename);
}
I'm having trouble moving all the files in the directory into a zip file. Below is my code. When I run the code, no zip file is created. What am I doing wrong?
$dir = "archive";
$files = array_diff(scandir($dir), array('.', '..'));
$zip = new ZipArchive();
$zipfile = "./test.zip";
$zip->open($zipfile, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
I have no idea if this is possible since there is no information available on the internet. I spend two hours searching without any result.
I am trying to zip a directory without knowing the file names that are in the directory. I tried many methods but they include the whole path, I only want the files in the zip.
I have tried the following:
$zipFile = "./testZip.zip";
$zipArchive = new ZipArchive();
if (!$zipArchive->open($zipFile, ZIPARCHIVE::OVERWRITE))
die("Failed to create archive\n");
$zipArchive->addGlob("./*.txt");
if (!$zipArchive->status == ZIPARCHIVE::ER_OK)
echo "Failed to write files to zip\n";
$zipArchive->close();
and:
$zip = new ZipArchive;
$zip->open('myzip.zip', ZipArchive::CREATE);
foreach (glob("target_folder/*") as $file) {
$zip->addFile($file);
if ($file != 'target_folder/important.txt') unlink($file);
}
$zip->close();
I have the next folder with files:
2147485934: {image.ai, image.jpg, image.psd}
And I want to create a new zip with thoses files:
<?
$zip = new ZipArchive;
$zip->open('newPack.zip', ZipArchive::CREATE);
foreach (glob("2147485934/*") as $file) {
$zip->addFile($file);
}
$zip->close();
?>
It works, but the new zip file have the same structure:
newPack.zip --> 2147485934: {image.ai, image.jpg, image.psd}
and I want to get the next structure inside my zip:
newPack.zip --> image.ai, image.jpg, image.psd //without folder 2147485934
Any help with this problem would be much appreciated
Try this:
$zip->addFile($file, basename($file));
The second argument defines the name of the file inside the archive. The code I added strips the directory name from the filename inside the archive.
Move to the selected folder with chdir:
http://www.php.net/chdir
<?
$zip = new ZipArchive;
$zip->open('newPack.zip', ZipArchive::CREATE);
chdir('2147485934')
foreach (glob("*") as $file) {
$zip->addFile($file);
}
$zip->close();
?>