PHP creating zip archive - php

I am creating a zip archive, but it doesn't work. There is no error logged into Error_log.txt. I am requesting a zip archive over ajax and response from ajax is empty, the $_SESSION['uid'] code looks like this.
$folder = md5($array->id);
$this->deleteIfExists($folder);
$zip = new ZipArchive();
$name = "users/".$folder."/".$_SESSION['uid'].".zip";
if($zip->open("users/".$folder."/".$_SESSION['uid'].".zip", ZIPARCHIVE::CREATE) != TRUE){
echo "Doesn't work :-(";
}
$zip->addFile("users/".$folder."/client_agree/original_".$array->id.".jpg", "agree.jpg");
/*
foreach(json_decode($array->imgs) as $key => $val){
$zip->addFile("users/".$folder."/client_agree/original_".$val.".jpg", $val.".jpg");
}
*/
$zip->close();
echo $_SESSION['uid'];

Related

Problem in down loading all certificates in Php(moodle)

Code to download all certificates:
<?php
require_once('../../config.php');
global $DB,$CFG;
$certlist = $_POST['select_cert'];
print_r($certlist);
$files = array('niBMkaooT.jpg');
$zip = new ZipArchive();
$zip_name = time().".zip";
$zip->open($zip_name, ZipArchive::CREATE);
foreach ($files as $file) {
$path = $file;
if(file_exists($path)){
$zip->addFromString(basename($path), file_get_contents($path));
}
else{
echo"file does not exist";
}
}
$zip->close();
?>
if($certificate!=''){
echo "<input type='checkbox' class='checkboxcert' name='select_cert[]' value='$certificate'>";
}
echo "</td>";
echo "<td>";
Also below i am getting $certificate and when i am downloading individual certificates this is working fine . But when selecting multiple document i am not able to download all
$certificate = get_certificate($userid,$c_id);
Please find the array which i have printed (print_r($certlist))
Array ( [0] => https://google.com/lms/plufile.php/69402/mod_certificate/issue/484123/Abu 2021_Abu, Neglecting, and Exploitation.pdf [1] =>
Please advise what changes are required?`
The certificate PDF isn't always saved, so the file might not always be available
I'd suggest creating and saving the PDF in your own code
Have a look at how the PDF is saved in the view code
https://github.com/mdjnelson/moodle-mod_certificate/blob/master/view.php
if ($certificate->savecert == 1) {
certificate_save_pdf($filecontents, $certrecord->id, $filename, $context->id);
}
Then work backwards from there to see how the variables are created
eg. $USER is the current user
$certrecord = certificate_get_issue($course, $USER, $certificate, $cm);
So you will need to replace that with the required $user in your code
$certrecord = certificate_get_issue($course, $user, $certificate, $cm);

Can't unzip file with php

I have a folder in my web server were I put zip files that I need to then unzip. I want to do that with php and this is what I have tried but it does not work:
<?php
$file = $_GET["file"];
$zip = new ZipArchive;
$res = $zip->open($file+'.zip');
$zip->extractTo('./');
$zip->close();
?>
The zip files are in the same folder as the php file, but when I go to the php page it does nothing.
By doing some testing I have found out that the script dies on the $zip = new ZipArchive; line
How can I manage this to work?
<?php
$fileName = $_GET['file']; // get file name in the URL param "file"
if (isset($fileName)) { // if $fileName php variable is set than
$zip = new ZipArchive; // create object
$res = $zip->open($fileName); // open archive
if ($res === TRUE) {
$zip->extractTo('./'); // extract contents to destination directory
$zip->close(); //close the archieve
echo 'Extracted file "'.$fileName.'"';
} else {
echo 'Cannot find the file name "'.$fileName.'" (the file name should include extension (.zip, ...))';
}
}
else {
echo 'Please set file name in the "file" param';
}
?>
Note:- For More Details Please refer https://www.php.net/manual/en/class.ziparchive.php
I have found the problem.
The code is fine, but the hosting service is not, and they do not have the ZIP extension available right now
Try this code. Also change $zip->open($file+".zip"); to $zip->open($file);.
+ (plus sign) is not concatenation operator in php
<?php
// $_GET["file"] is set to `a.zip`
$file = $_GET["file"];
$zip = new ZipArchive;
$res = $zip->open($file);
$zip->extractTo('./');
$zip->close();
?>

Unable to open zip with ZipArchive

I'm running a php script (like bash).
This script do this:
Download a zip file from FTP server
ftp_get($ftp, $myFile.zip, $file, FTP_BINARY);
When I have downloaded the file, I want to extract its content:
$zip = new ZipArchive;
$res = $zip->open($myFile);
if ($res === TRUE) {
$zip->extractTo($extractDir);
$zip->close();
} else {
dump( 'error, code:' . $res);
die;
}
But, I get the error 19 who is: "Not a zip archive"
If I open the .zip, it is a compressed archive because I have the PK key...
Does anyone have a solution or idea ?
Regards
Solutions
I am using this code and it work fine.
$zip = new ZipArchive;
$myfileDir = './abc.zip';
$extractDir = './';
$res = $zip->open($myfileDir);
if ($res === TRUE) {
$zip->extractTo($extractDir);
$zip->close();
echo 'Successfully extract the file';
} else {
echo ('error, code:' . $res);
die;
}
Question
I believe the problem is because of the zip file, would upload the the zip folder to google drive and let us download and test it locally?

Zip Not Being Created

I am trying to create a page that simply zips files, that's all there is to it. I added the check to make sure the method was actually being called, and it is, I am seeing the "ok" when the code is run, however, no ZIP file is being created.
Any help would be appreciated. I can confirm that those files exist. (They are in relation to where the php file is)
$zip = new ZipArchive;
if ($zip->open('/files/custom.zip', ZipArchive::CREATE) === TRUE) {
$zip->addFile('/files/textures/16/default/pack.mcmeta');
$zip->addFile('/files/textures/16/default/pack.png');
$zip->close();
echo "ok";
} else {
echo "failed";
}
try this
<?php
$zip = new ZipArchive;
if ($zip->open('/files/custom.zip') === TRUE) {
$zip->addFile('/files/textures/16/default/pack.mcmeta', 'pack.png');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>

Get ZIP file from generated URL

I have a URL that, when requested, start the download of a .zip file. The URL looks something like this:
http://website.nl/servlets/ogexport?koppeling=WEBSITE&user=&password=&og=BOG&kantoor=**
How can I make sure that the zip file that gets generated in that proces get used in my PHP unzip script?
$file = 'testing.zip';
$path = pathinfo(realpath($file), PATHINFO_DIRNAME).'/xml';
// unzippen
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
$zip->extractTo($path);
$zip->close();
echo 'success';
// debug("ZIP bestand uitgepakt", 2);
} else {
// debug("ZIP niet uitgepakt", 2);
echo 'something went wrong.';
}
$xml_path = $path."/testing.xml";
echo $file.'<br>';
echo $xml_path;
Hoping for help!
I would recommend using php curl to download the remote file.Here is an example

Categories