I found this script on stackoverflow but am having an issue. The zip is being created with the file I am telling it to but it's also zipping the lower directories to that file. The zipped file contains: \uploads\1\assets\2\ai\filename.ai
$zip = new ZipArchive();
$zip->open('uploads/1/assets/2/ai/filename.zip', ZIPARCHIVE::CREATE);
$zip->addFile('uploads/1/assets/2/ai/filename.ai');
$zip->close();
$zip->addFile('uploads/1/assets/2/ai/filename.ai', 'filename.ai');
The second parameter is the localname:
If supplied, this is the local name inside the ZIP archive that will
override the filename.
If you leave off the directory path, it will not be included in the zip file.
Related
I have these few lines that create an empty zip file that I will put things in.
$zip = new ZipArchive();
$generatedname = uniqid().'.zip';
$res = $zip->open('tmp/'.$generatedname, ZipArchive::CREATE);
$res is 5, which corresponds with a read error, and when I go to add files I get an error that the zip is not initialized. This code works on my local machine but not on my iis server, so it's some kind of configuration error?
I can read and write files with fopen and fwrite, so I don't think is has to do with rw permissions, so I'm kinda out of troubleshooting ideas.
Ok, I actually figured this one out.
The issue is actually with where the zip is being created. I had it set to /tmp in my working directory since I was going to have the file downloaded and then immediately deleted.
So I found this article that talks about using a directory the will always be writable by php, so creating my zip file in the system temp directory seems to have done the trick. Heres the updated code:
chdir( sys_get_temp_dir() );
$zip = new ZipArchive();
$generatedname = uniqid().'.zip';
$zip->open($generatedname, ZipArchive::CREATE);
My file structure in zip is
lib
css
js
app1
app2
index.php
All the above files and folders are in test.zip
i want to extract this test.zip using php
$zip = new ZipArchive;
$res = $zip->open("test.zip");
if ($res === TRUE)
{
$zip->extractTo('path');
$zip->close();
}
if lib folder is already exist in the directory where we are unzipping then except lib(folder) all other files and folders should get override.
how to solve this.
http://php.net/manual/en/ziparchive.open.php , on open, pass in the over write flag.
Attempting to create a kmz file from a kml file. The script was working last week, but somehow it stopped working. I am attempting to troubleshoot this issue by creating a .zip file which contains a .kml file.
The process is as follows
1. Kml file is created
2. .zip file is added
3. kml file is added to zip according to name
4. .zip is saved
The issue is that when I open the directory the .zip contains that looks like the file structure of the .kml file, not just the file.
For example if the .kml was inside c:/foldera/folderb. The zip would contain c:/foldera/folderb/kml.
$zip = new ZipArchive();
$zip_name = $_SERVER['DOCUMENT_ROOT'] .'/assets/kml'. "/r".$r.".zip";
$filename = $_SERVER['DOCUMENT_ROOT'] .'/assets/kml'. "/r".$r.".kml";
$zip->open($zip_name, ZIPARCHIVE::CREATE);
$zip->addFile($filename);
$zip->close();
unlink($_SERVER['DOCUMENT_ROOT'] .'/assets/kml'. "/r".$r.".kml");
For some reason you have two addfile's:
$zip->addFile($filename, ltrim($filename, '/'));
$zip->addFile($filename);
Try replacing those two lines with this:
$zip->addFile($filename, basename($filename));
basename() returns only the filename, removing the path - http://www.php.net/manual/en/function.basename.php
I'm having some issues, I'm using PCLZip to create an archive. I don't get any errors, the zip file is created, but when I go to view it, the archive is empty and on my windows machine I get an error "The Compressed (zipped) folder "local directory zip file") is invalid. I have the following code:
$dir = '../downloads/liability/';
$archive = new PclZip($dir.'archive.zip');
$v_list = $archive->create($dir);
if ($v_list == 0) {
die("Error : ".$archive->errorInfo(true));
}
My directory structure is:
-admin
--liabilityDev.php (where the above code resides)
--index.php
--commission.php
-downloads
--liability
---one.pdf
---two.pdf
The end result is that in the liability folder, there is a file called archive.zip which contains the 2 pdf's but I get the invalid error.
If I don't have the directory variable, I archive index.php and commission.php and that works fine. It leads me to believe it may be a permission issue, but I'm running on fumes now. Please help!
You can try this:
if(extension_loaded('zip')){
$zip = new ZipArchive();
if($zip->open('../downloads/liability/archive.zip', ZIPARCHIVE::CREATE)===TRUE){
$zip->addFile('path of any normal file to be add into zip');
}
$zip->close();
}
I think, this will fullfill your need. Before implementing this code, please check first that, zip extension is already loaded or not.
I am creating a zip archive in php and want to add some pdf files available in my pdfs_lib folder.I have successfully created the archive but there is a problem that inside the created zip archive, files get added into pdfs_lib folder, which is not what i want.
I want to add pdf files present in pdfs_lib folder directly into the archive instead of adding those files inside pdfs_lib folder in my created zip archive.
What i have tried is:
// Create Zip File
$zip = new ZipArchive;
$res = $zip->open("pdfs_lib/my_zip_archive.zip", ZipArchive::CREATE);
if ($res === TRUE)
{
// Add pdf file to zip archive
$zip->addFile("pdfs_lib/pdf_file_1.pdf");
$zip->addFile("pdfs_lib/pdf_file_2.pdf");
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
This code creates the zip archive successfully but whwen i unzip the archive it creates a pdfs_lib folder and unzip all files inside this newly created folder.
Any suggestion on how to create a zip archive without add pdf_lib folder in it.
Change the addFile calls to:
$zip->addFile("pdfs_lib/pdf_file_1.pdf", "pdf_file_1.pdf");
The second parameter is the 'localname', the name used in the zipfile itself. If you leave out the folderpath there, it will be added to the root in the zipfile.
A side not as an into: usually it is considered good practice if an archive (be it zip or any other archive format) unpacks into a clean folder instead of littering the contained files all over the place.
However if you really want to implement this without such folder, then you must rework your code. You have two alternatives:
you can change the working directory your php script is executed in into that folder where you keep the files in. In that case you do not need any path to add files to the zip archive, thus no folder is constructed inside the archive.
the addFile() method of phps zip extension allows to specify a second (optional) argument which allows to pass the target name of a passed file.