Files Inside Zip Without Path - php

I create a zip file with php with this code:
<?php
$file1 = '/home/##ACCOUNT##/public_html/##SITE##/images/uploadfiles/backup/file1.csv';
$file2 = '/home/##ACCOUNT##/public_html/##SITE##/images/uploadfiles/backup/file2.csv';
// CREATE THE ZIP
$files = array($file1, $file2);
$zipname = '/home/##ACCOUNT##/public_html/##SITE##/images/uploadfiles/backup/backup.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
?>
In the created zip i get 2 files as i want it but it also contains the tree/path of those files so opening the zip you have to navigate inside those folder to get to the files is there a away to just have the 2 files in the zip file1.csv & file2.csv without the folders?
I found the anwser i wanted in another post
php creating zips without path to files inside the zip

As Workaround use $localname (second parameter) to define and control file/directory structure inside the zip eg.
<?php
$zip->addFile($file, $localname_relative_path);
?>

You can use ZipArchive::renameName method to change name of the file being added or use ZipArchive::addFromString. In the second case you need to remember that you first need to read that file into memory, so it should only be used in consideration with memory available.
Also you can use second parameter to ZipArchive::addFile as suggested in other answer.
foreach ($files as $file) {
$localName = basename($file);
$zip->addFile($file, $localname);
}

Related

How to copy the source code of my website recursively into a zip file and download using PHP

Since my file manager doesn't allow me to download multiple files, instead only allowing me to download them one by one (which is tedious, and, eventually will become inefficient), I want to know how to download all my website file contents into a single zip folder. I found a code that works from geeksForGeeks, however it only zips on that current directory level (not recursively). I want every file on my website put into a zip folder while preserving their place in their corresponding folders.
The code I found:
// Enter the name of directory
$pathdir = "./";
// Enter the name to creating zipped directory
$zipcreated = "BackupFiles.zip";
// Create new zip class
$zip = new ZipArchive;
if($zip -> open($zipcreated, ZipArchive::CREATE ) === TRUE) {
// Store the path into the variable
$dir = opendir($pathdir);
while($file = readdir($dir)) {
if(is_file($pathdir.$file)) {
$zip -> addFile($pathdir.$file, $file);
}
}
$zip ->close();
}
How do I add the folders as well? It only zips the files of the current directory and not all the subfolders.

Deleting all files created by script after finsihing script php

Is there a way to delete all files created by running the script. I have written a code were a lot of xml files are created. They have to be stored till the script run through all the xml files. These files aren't necessary any more when the script is finished. Is there a possibility to write a code that will delete these files in the same script?
I used unlike(filname) in my loop by saving the xml files locally. The problem is that it removed my file before I could used in the next part of my code.
foreach ($links as $lin){
unlike($filename);
}
Add on the end of script-file a code like this:
$files = glob('path/to/files/dir/*');
foreach($files as $file){
if(is_file($file)) {
unlink($file);
}
}
Add the following script
$files = glob('path/to/temp/*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file))
unlink($file); // delete file
}
If you want to remove 'hidden' files like .htaccess, you have to use
$files = glob('path/to/temp/{,.}*', GLOB_BRACE);

zip Folder without using getRealPath

I'm trying to make zip file from a folder inside my localhost server which is my computer with windows 7 OS to a host .The problem is with using getRealPath for making the zip file the zip file content would be like this "D:\xampp\htdocs\mobl\admin\downloads" but I don't want those folders inside my zip file.
How should I make zip file without those folders and just the target folder ?
this is the code I'm using to zip the folder
enter code here
$zip = new ZipArchive;
$zip->open('downloads/'.$zipname,ZipArchive::CREATE);
// Initialize empty "delete list"
$filesToDelete = array();
// Create recursive directory iterator
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY);
foreach($files as $name => $file) {
// Get real path for current file
$filePath = $file->getRealPath();
// Add current file to archive
$zip->addFile($filePath);
}
// Zip archive will be created only after closing object
$zip->close();
Thanks

Zipping up a folder without extra folders in the zipped file

So, I understand it's pretty easy to zip a folder and its contents given that php.net and stackoverflow are full of sample codes. I am trying to zip up a folder as well. This is my code:
$zip = new ZipArchive;
$zip->open('myzip.zip', ZipArchive::CREATE);
foreach (glob("/Volumes/Data/Users/username/Desktop/Archive/some/thisFolder/*") as $file) {
$zip->addFile($file);
}
$zip->close();
My script is running in a folder, say on Desktop, and I want to zip thisFolder and its contents. The above code WORKS. But, the problem is that when I unzip the myzip.zip created, I get a structure like
Volumes>Data>Users>username>Desktop>Archive>some>thisFolder
and then I find the contents inside the 8th folder (thisFolder) in this case. How can I change this code so that when I unzip myzip.zip,I would straightaway see the folder thisFolder with the contents inside it instead of having to navigate through folders 7 times before getting my content?
I tried to change the path and silly things like that. But, it doesn't work.
Thanks
If you want everything in the file to be a name relative to your starting folder, use chdir() to start from there:
chdir("/Volumes/Data/Users/username/Desktop/Archive/some/thisFolder");
foreach (glob("*") as $file) {
$zip->addFile($file);
}
Actually, I don't think this will work when $file is a subdirectory -- addFile doesn't recurse automatically. There's a comment in the documentation that shows how to write a recursive zip function:
http://www.php.net/manual/en/ziparchive.addemptydir.php
bool ZipArchive::addFile ( string $filename [, string $localname ] )
filename
The path to the file to add.
localname
local name inside ZIP archive.
You can use the pathinfo function to each $file in your loop, in order to retrieve the filename without the path, and then, use it as second parameter to the addFile method.
pathinfo doc here
Here is an example that could solve your problem:
// Create your zip archive
$zip = new ZipArchive;
// open it in creation mode
$zip->open('myzip.zip', ZipArchive::CREATE);
// Loop on all your file
$mask = "/Volumes/Data/Users/username/Desktop/Archive/some/thisFolder";
$allAvailableFiles = glob($mask . "/*")
foreach ($allAvailableFiles as $file)
{
// Retrieve pathinfo
$info = pathinfo($file);
// Generate the internal directory
$internalDir = str_replace($mask, "", $info['dirname']);
// Add the file with internal directory
$zip->addFile($file, $internalDir . "/" . $info['basename']);
}
$zip->close();

php: can i zip glob files without full path?

this code works like a magic batch of fresh potato chips on a really bad day: PURE JOY. but when i extract the archive, the files are nested in the path they were stored in on the server. me no likey. how do i zip the glob without the full path; just the files.
$zip = new ZipArchive();
$zip->open("_dox/pdftmp/".$_SESSION['archiveName'], ZipArchive::CREATE);
$files_pdf = glob('_dox/pdftmp/*.pdf');
foreach ($files_pdf as $file)
{
$zip->addFile($file);
}
$zip->close();
remember: just files, no extra folders.
WR!
Specify the "local name" that the files will have inside the zip. See the addFile() docs.
$zip->addFile($file, basename($file));

Categories