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?
Related
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();
}
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
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'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.
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.