Creating a zip file in a specific path without any subfolders - php

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

Related

How to create a zipfile with child zipFiles in php

i need to create a zipFile with a childs zipFiles already exist :
> folder
- file1.zip
- file2.zip
I want to get a zip file containing all the zip files in the folder.
$dir = "path/to/my/dir";
$finder = new Finder();
$zip = new \ZipArchive();
/***** I create a empty zip file ****/
$filesystem->dumpFile("$dir/delivrables.zip",'');
if($zip->open('delivrables.zip', ZipArchive::CREATE) === TRUE) {
foreach ($finder->in($dir) as $file) {
$zip->addFile($file->getPath(), $file->getFilename());
}
}
I don't get error but i can't extract zip file.
You can create a zip file with zip files in the same way as you create a zip file with images, documents etc

deleting all files from a folder, excluding some files from being deleted

Following this thread (first post) I have successfully accomplished the task of deleting all files from a folder using php.
This is the code I use:
$files = glob('path/to/temp/*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file))
unlink($file); // delete file
}
I would like to exclude some files from being deleted. What code adjustment should I apply?
$files = glob('path/to/temp/*'); // get all file names
$exceptions = ["awesomefile_a", "awesomefile_b"];
foreach($files as $file){ // iterate files
if(is_file($file) && !in_array(end(explode("/", $file)), $exceptions))
unlink($file); // delete file
}

Create a zip in specific Folder PHP

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.

Copy folder content to Zip php

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

create zip file inside a folder but without folder inside that zip

$File = "images/files.txt";
$zip = new ZipArchive();
$filename = "./images/files.zip";
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$zip->addFile("$File");
$zip->close();
This code creates a files.zip file inside 'images' folder, if I open that zip file, 'images' folder is there too. I don't want folder 'images' to be there. But only the 'files.txt' file(located inside images folder) needs to be there.
Files structure:
zip.php
images
files.txt
How can I do that?
#hek2mgl I have 'files.txt' inside 'images' folder, that's why it's happening
Then your code will not work at all as the path to $File is wrong. Use this:
$File = "images/files.txt";
$zip = new ZipArchive();
$filename = "./images/files.zip";
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
// use the second parameter of addFile(). It will give the file
// a new name inside the archive. basename() returns the filename
// from a given path
$zip->addFile("$File", basename($File));
if(!$zip->close()) {
die('failed to create archive');
}

Categories