PHP zipping files without saving to local directory - php

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

Related

Laravel : Extract ZipArchive file from storage external disk

I'm trying to extract a ZipArchive file located in an external storage disk in my laravel application. It is working on my local environment but not in production
Edit : my FILESYSTEM_DRIVER env variable is different on production and working (tested with others Storage functions)
I have a zip file located in Storage::path('folder/file.zip'); which i'm trying to extract in the same folder :
// path to the zip file
$path = Storage::path('folder/file.zip');
$zip = new ZipArchive;
if($zip->open($path) {
$zip->extract(Storage::path('folder')); // working in local but not in production on an external disk
$zip->close();
}
With this code, I receive a code 9 error : No such file
It says that my $path variable is wrong but I my debug Storage::exists('folder/file.zip'); returns true
I don't know where I am missing something. I could not find any helping answer on the web.
Any help would be appreciated.
Thanks in advance
$path = Storage::path('folder/file.zip');
$zip = new ZipArchive;
if($zip->open($path)) {
$zip->extract(Storage::path('folder')); // working in local but not in production on an external disk
$zip->close();
}

How to move image file on AWS S3 using Laravel?

I want to move a file from a folder to another folder on s3 but it is not moving to destination folder correctly. My code is below:
$old_path = "media_manager/omm/pic.jpg"
$new_path = "media_manager/omm/mypics/pic.jpg"
Storage::disk('s3')->move($old_path, $new_path)
It returns true and image file disappeared in old(source) folder but it is not showing up in destination(new) folder. I dont know why? I am using ajax to do this.
Plz help in this regard. Thanks
is the mypics folder exists on AWS S3 Bucket?
if no try to add the folder "mypics" manually and make sure that you have the permission to write he file inside of it

Creating Zip returns read error

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

how to get only folder name when extract the zip file in code igniter

I am using zip library to zip a folder or directory
My path structure is
$estructure = "/home/administrator/Downloads/pdf_files/"
And iam zipping folder using below code
$this->zip->read_dir($estructure);
$this->zip->archive('/var/www/');
$this->zip->download('folder_name.zip');
Now problem is when I extract compressed directory it gives full path like
/home/administrator/Downloads/pdf_files/
what I need is when I extract the zip file its should only diretcory like pdf_file,
Could any help me on that please? thanks
Ok I got answer
$this->zip->read_dir($estructure,FALSE);
ob_end_clean();
Its working fine now.

PHP compress files into zip

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.

Categories