Creating zip file with php - php

I am creating a zip file of a particular folder, I am using joomla 1.5 version in which i have a component of form submission and I want to create a zip file with submitted documents. On my localhost, its creating the zip file, when I test to remote server, It replaces the extension with some kind of a number, suppose we are creating a file test.zip, but its creating like this test.zip.a12345 and test.zip.b12345..
The code which I have used to create zip file:
$files = array(
'files/file1.jpg',
'files/file2.jpg',
'files/file3.jpg'
);
$zip = new ZipArchive();
$zip_name = "zipfile.zip";
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){
$error .= "* Sorry ZIP creation failed at this time";
}
foreach($files as $file){
$zip->addFile($file);
}
$zip->close();
and the output on local server is zipfile.zip
output on live server is : zipfile.zip.a11236 and zipfile.zip.b11236

You probably need to use the ZipArchive::OVERWRITE flag when opening the file.
It chooses another name because the file already exists?
See: http://php.net/manual/en/zip.constants.php

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.

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!

Extract Zip File from remote server with white-space in file name

I have a list of zip files to read from a remote server. The list of the files are as follows:
CA-EN Bosch 20170901.zip
CA-EN Gaggenau 20170901.zip
CA-EN Thermador 20170901.zip
As we can see here, all filenames are with white-space in it. Due to this My code is unable to extract these zip files.
Following is what I am using to extract file contents:
$destination = '../../boschzip';
$zip = new ZipArchive;
$res = $zip->open('CA-EN Bosch 20170901.zip'); // Your filename
if ($res === TRUE) {
$zip->extractTo('../../boschzip');
$zip->close();die;
}
else{
echo 'Error Message. :(';die;
}
I can not make any change in the file name as it is on the client's server. So is it possible to extract these zip files with white-space in their file names.

PHP how to remove author metadata from docx

In PHP I've to remove the author metadata from a DOCX file. First of all, I've unzip the document and after I've edit the author metadata setting it empty using an XML reader library.
$file = 'document.docx';
$filename = 'path/'.$file;
// Unzip the docx
$unzipped = md5($filename);
if (file_exists($unzipped)) {
rmdir($unzipped);
mkdir($unzipped);
}
$zip = new ZipArchive;
$res = $zip->open($filename);
if ($res === true) {
extractFolder($zip, "word/media", $unzipped);
$zip->extractTo($unzipped);
$zip->close();
} else {
die("The docx file appears to be corrupt (i.e. it can't be opened using Zip).\n");
}
The problem occurs when I tried to re-zip the document. In fact, when the re-zip is done, the docx file seems to be corrupted when I open it in Word.
create_docx($unzipped,'_zips/'.$file );
How can I modify the author metadata (setting the document anonymous) and save the correct docx file?
I don't know php, but I assume the problem is that you have to compress inside the folder $unzipped, NOT the folder itself. So a kind of recursive "$unzipped/*" as source instead of "$unzipped".

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

Categories