PHP compress files into zip - php

Hello there
okay so this is the deal i have a folder where my mp3 files are stored for single download, and now i want a feature where you can download all the mp3s at once in a zipped folder. Is it possible to zip all the files from that folder using php ( or anything really but if not PHP then please explain thoroughly) or would i have to just re-upload all the mp3s again in zipped format?
Thanks you so much.

Just to demonstrate use of the undocumented addGlob() method of the zipArchive class:
$zipFile = dirname(__FILE__)."/myMP3s.zip";
$zipArchive = new ZipArchive();
if (!$zipArchive->open($zipFile, ZIPARCHIVE::OVERWRITE))
die("Failed to create archive\n");
$zipArchive->addGlob(dirname(__FILE__)."/*.mp3");
if (!$zipArchive->status == ZIPARCHIVE::ER_OK)
echo "Failed to write local files to zip\n";
$zipArchive->close();

There are several PHP libraries for compression. Have you tried them?

http://php.net/manual/en/book.zip.php
Mark Baker's example looks good, read up if you want to learn or need something more specific.

Related

PHP zipping files without saving to local directory

I want to zip some images using Laravel, but I can't figure out how to zip those images without storing to local directory first. Is it possible doing so without saving them to my local directory? I have PHP 7.1 running on my local machine.
From official docs of ZipArchive, the file could only be add to the zip if it's already stored in local directory.
Here is some snippets of my code for zipping a single image:
Use Image;
Use ZipArchive();
$image = Image::make('http://example-image.com');
$zip = new ZipArchive();
$zip->addFile($image, 'image-filename');
Expected behaviour: The image will be inserted to the $zip.
Any help would be appreciated. Thanks!
Try using addFromString:
$zip->addFromString('image-filename.png', $image->encode('png'));
or
$zip->addFromString('image-filename.png', $image->encode('jpg', $quality));
Try :
$zip->addFile($_FILE['tmp_name'], $_FILE['name']);
add to zip from the temp location

Ghostscript: create extra folder for output JPEG

I have the following code:
<?php
$path = "/path/file/";
$pathout = "/path/out/file/";
exec('convert '.$path.'test_pdf.pdf[0] '.$path.$value.'.jpg');
?>
I want to know if there is a way that ghostscript generates the output JPG and at the same time also generates an new output folder?
I believe it can not.
Actually you are running a script. In Script, if the target directory does not exist, you will receive an error, like "Not a directory".
Ghostscript does not create directories, so no, it can't do that.
No, Ghostscript does not create new directories. It only creates new files. It can only write these files to existing directories.
It is the task of your script to prepare everything. Your script needs to create the new folder in advance where you want Ghostscript to write the output to.

Corrupt ZIP files using PHP & PCLZip

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.

Google app engine php zip extension

I am using Google app engine and I want to create a zip archive using the zip extension which is supported in the current version. The problem is that every time i call ZipArchive::close it returns false and ZipArchive::getStatusString returns "Failure to create temporary file: Read-only file system". I am creating the zip file in the cloud storage like this:
$zip = new ZipArchive();
$zip->open("gs://whatever/somethig.zip", ZipArchive::OVERWRITE); //returns true
$zip->addFromString('file.txt', "some random data"); //returns true
$zip->numFiles; // = 1
$zip->close(); // returns false
$zip->getStatusString(); //returns Failure to create temporary file: Read-only file system
and it should be writable, it actually is with file_set_content or whatever. I tried the same code with a couple of variations:
already existing zip file;
not existing zip file;
ZipArchive::CREATE as a second parameter of the ZipArchive::open call
Using ZipArchive::addFile to add the file from cloud storage
Maybe it tries to create a temporary file on some other place not on the cloud storage, but this thing that i'm doing is very common scenario and i doubt that this is an app engine bug, after all they claim that they support the zip extension here.
Does someone have some idea what can be the problem?
Thank you.
Looks like a bug - can you open an issue in the issue tracker?

ci zip encoding class not working properly

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

Categories