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();
}
Related
Im using a local (rhel 8) server to unzip a file using php (solution proposed in : https://stackoverflow.com/a/8889126/9583635)
<?php
// assuming file.zip is in the same directory as the executing script.
$file = 'temperatures.zip';
// get the absolute path to $file
$path = pathinfo(realpath($file), PATHINFO_DIRNAME);
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
// extract it to the path we determined above
$zip->extractTo($path);
$zip->close();
echo "Success! $file extracted to $path";
} else {
echo "Failled! I couldn't open $file";
}
?>
The problem is:
when I execute this code on the server using:
php /var/www/html/unzip.php
it works fine (success message) and I find the extracted file in the same directory.
but when I use browser (http://192.168.1.5/unzip.php) the success message is printed but I can't find the unziped file in this directory.
note: I tried using wamp server and it works, so the problem may be in the server.
By the way all the php line codes that were manipulating files wasen't taking affect. I solved this by allowing the Apache user (apache in rhel) to create files in the directory. This can be done by making Apache the owner of the directory running this cmd:
sudo chown apache /var/www/html/
After moving a website from one server to another, both running IIS, when a zip file creation script is called the file created has some random characters at the end, ex. what should be 'test.zip' is converted to 'test.zip08c1b35d' giving error when trying to download them.
I've discovered that it only happens inside the website path, if I create a symbolic link inside the website path with the same name, pointing to a folder outside the website path the zip file is created with the correct name. Any other folder within the website path the name is changed.
The zip file format is correct as if I delete the characters and leave the name ending '.zip' the file opens fine.
The code used is:
$dir ='../mylims/test/';
$destination = $dir.'test.zip';
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
echo "Error";
}
$zip->addFile('./test/testfile.txt','testfile.txt');
$zip->close();
Any suggestions what to look? Tks
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.