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.
Related
After moving a website from one server to another, both running IIS, when a zip file creation script is called the file created has some random characters at the end, ex. what should be 'test.zip' is converted to 'test.zip08c1b35d' giving error when trying to download them.
I've discovered that it only happens inside the website path, if I create a symbolic link inside the website path with the same name, pointing to a folder outside the website path the zip file is created with the correct name. Any other folder within the website path the name is changed.
The zip file format is correct as if I delete the characters and leave the name ending '.zip' the file opens fine.
The code used is:
$dir ='../mylims/test/';
$destination = $dir.'test.zip';
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
echo "Error";
}
$zip->addFile('./test/testfile.txt','testfile.txt');
$zip->close();
Any suggestions what to look? Tks
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);
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.
I want to crate a zip file and download it.
So my code is as follow;
Since i am using ci zip encoding class
my folder structure is like
c:/xampp/htdocs/gallery/print/oid_1/1.jpg,2.jpg,3.jpg
My code
$path = getcwd().'/print/oid_1/';
$this->zip->read_dir($path,FALSE);
// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('oid_1.zip');
Here is my problem,
l C:\Users\instinctclassic\Downloads\Compressed\oid_1.zip: The archive is either in unknown format or damaged so what does this means -does it means it was mistaken while making zip file or it was mistaken while downloading.For downloading mistake i have downloaded the zip file many time enough to be sure.
2 I repaired the downloaded zip file(oid_1) and extract it but the extracted folder structure is not ignored before the oid_1 folder as said in ci zip encoding class tutorial
$this->zip->read_dir($path, FALSE);
//This will create a ZIP with the folder "directory" inside, then all sub-folders stored correctly inside that, but will not include the folders /path/to/your.
3 Assuming my folder oid_1 already exists somewhere on server.
I know this question is previously asked but mine exist all the problem from making zip file to extracting.So i am sorry for this.
Thanks
Try directory path like this:
$path = getcwd().'\print\oid_1\';
this way path will be proper, because getcwd() will return you c:\xampp\htdocs\gallery
I have a zip file and i used the following code to extract that zip and put all extracted file in another location.
$zip = new ZipArchive;
echo $zip;
if ($zip->open("$pwd/wordpress-3.4.2.zip") === TRUE) {
$zip->extractTo("$pwd/Repo/");
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
But i cant any extracted file whether it show 'ok' in browser. What is the mistake in the code i cant found. another thing to download the zip file from the "http://wordpress.org/latest.zip" site. i used the code written bellow. Here also i cant download the file.
$foo = system('wget http://www.myserver.com/file.txt ~',$output);
You could also use:
exec('wget http://wordpress.org/latest.zip -O temp.zip');
exec('unzip temp.zip -d /somedir');
It appears that the ZipArchive needs a reference to the library containing that class.
Also make sure that in your php.ini file, you have enabled the library that enables zip files functions to work on your server. (The zip extension is loaded by default under PHP 5.3)
Again, make sure the directory you want to extract the files has write permissions.