php: can i zip glob files without full path? - php

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

Related

Unable to extract zip file which generated using php

I have written the PHP script to generate the zip file. it's working fine when I use rar software to extract it but not getting extract with rar software. I can't ask to users to install rar software to extract downloaded zip file.
I don't know where i am commiting mistakes.
Here i attached error screen shot which i get when try to open zip file.
// Here is code snippet
$obj->create_zip($files_to_zip, $dir . '/download.zip');
// Code for create_zip function
//create the archive
$zip = new ZipArchive();
if ($zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach ($valid_files as $file) {
$filearr = explode('/', $file);
$zip->addFile($file, end($filearr));
}
$zip->close();
If $valid_files is a glob'd array, use basename() instead of end(), your zip might not actually have added any files causing for it to be an invalid zip (however that would be visible in the size of the zip file).
Also try winrar/winzip/7zip and see what they return, microsoft's internal zip engine might not be up to date enough to open the zips.
I have also encountered this problem, using 7z solved the problem but we need to send the zip to somebody else so 7z is a nono.
I found that, in my case it is that the file path is too long:
When I use this:
$zip->addFile($files_path.'/people.txt');
And it generated a zip folder nested very deep e.g. ["/tmp/something/something1/something2/people.txt"]
So I need to use this instead
$zip->addFile($files_path.'/people.txt', 'people.txt');
Which generate a a zip folder with only 1 layer ["people.txt"], and Windows Zip read perfectly~
Hope this helps somebody that also have this problem!

Files Inside Zip Without Path

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

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 save zip to disk

I want to zip files uploaded by a user to a certain Location.
While the upload part works, I have no Idea how to zip the file. I am able to create a zip, but do not now how to actually save it to a disk.
Simply pass the desired destination path to the zip when creating it:
$path = 'path/to/file.zip';
$zip = new ZipArchive();
$zip->open($path, ZipArchive::CREATE);
// add contents...
$zip->close();
This is gonna surely help you out....These are 2 well explained examples hope it helps ..
http://www.9lessons.info/2012/06/creating-zip-file-with-php.html
http://davidwalsh.name/create-zip-php

Categories