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);
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
Trying to update a file file.xml, which is with folders dirA/dirB/dirC/file.xml where dirA is the current working dir. The file file.xml exists and has write permissions.
Using the following code works in local but on server it created a file by name "dirA\dirB\dirC\file.xml" outside dirA and saves into it
$file = fopen("dirA\dirB\dirC\file.xml", "w+")
fputs($file, $xmlFile);
fclose($file);
Any idea why?
Maybe because you are running another environment on your server?
Windows and Linux are a little bit itchy on their folders.
You may also check if you have to use backslashes or not!
Probably you also have to quote them:
$file = fopen("dirA\/dirB\/dirC\/file.xml", "w+");
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.
I found this script on stackoverflow but am having an issue. The zip is being created with the file I am telling it to but it's also zipping the lower directories to that file. The zipped file contains: \uploads\1\assets\2\ai\filename.ai
$zip = new ZipArchive();
$zip->open('uploads/1/assets/2/ai/filename.zip', ZIPARCHIVE::CREATE);
$zip->addFile('uploads/1/assets/2/ai/filename.ai');
$zip->close();
$zip->addFile('uploads/1/assets/2/ai/filename.ai', 'filename.ai');
The second parameter is the localname:
If supplied, this is the local name inside the ZIP archive that will
override the filename.
If you leave off the directory path, it will not be included in the zip file.