Hi,
I’m trying to create a zip archive with php using cron jobs. So far what I’ve done is get a json string from rabbitmq create pdf files and store into a dirctory, and then I need to add these files into a zip file so it can be downloaded in the future. I’ve tested the script calling it from the browser and it works fine but when i try to run it with cron jobs then it creates all the pdf files but doesn’t create the zip file. I believe the problem could be in the way cron jobs manage the directories. The php file will be outside the public directory, at the moment I’m testing it on my local machine using MAMP
This is the script:
// Load config file
require dirname(__DIR__, 2) . '/km-config/km-config.php';
// Load composer autoloader
require dirname(__DIR__) . '/km-composer/vendor/autoload.php';
// Load email functions
require dirname(__DIR__) . '/km-functions/km-email-functions.php';
// Load email functions
require dirname(__DIR__) . '/km-functions/km-string-functions.php';
//Connect to CloudAMQP server
$queue = new CloudAMQP;
$queue->CloudAMQP_PDF_Connect(AMQP_HOST, AMQP_PORT, AMQP_USER, AMQP_PASSWORD, AMQP_VHOST);
// $kmg_zip_directory_array = array();
// Process messages from the queue
$queue->CloudAMQP_PDF_Process(function($message) {
$messageBody = json_decode($message->body);
$kmg_user_firstname = $messageBody->kmg_pdf_user_firstname;
$kmg_user_lastname = $messageBody->kmg_pdf_user_lastname;
$kmg_user_username = $messageBody->kmg_pdf_user_username;
$kmg_user_password = $messageBody->kmg_pdf_user_password;
$kmg_zip_directory = $messageBody->kmg_pdf_zip_directory;
// Check if a subdirectory with subdomain name exst otherwise create one
if (!file_exists(dirname(__DIR__, 2).'/km-documents/'.$kmg_zip_directory)) {
// Make new directory with subdomain name
mkdir(dirname(__DIR__, 2).'/km-documents/'.$kmg_zip_directory, 0755, true);
if(!file_exists(dirname(__DIR__, 2).'/km-documents/'.$kmg_zip_directory.'/km-imported-owners-letters')){
// Make new directory with subdomain name
mkdir(dirname(__DIR__, 2).'/km-documents/'.$kmg_zip_directory.'/km-imported-owners-letters', 0755, true);
}
}else{
if(!file_exists(dirname(__DIR__, 2).'/km-documents/'.$kmg_zip_directory.'/km-imported-owners-letters')){
// Make new directory with subdomain name
mkdir(dirname(__DIR__, 2).'/km-documents/'.$kmg_zip_directory.'/km-imported-owners-letters', 0755, true);
}
}
// Make sure we decrypt password coming from CloudAMQP
$km_user_decrypted_password = km_encrypt_decrypt('decrypt', $kmg_user_password);
// Create file name structure
$filename = 'Lettera-benvenuto-'.$kmg_user_firstname.'-'.$kmg_user_lastname.'-'.time();
// Call class mpdf to create pdf
$mpdf = new \Mpdf\Mpdf();
// Load the email template
$km_email_message = file_get_contents(dirname(__DIR__, 2).'/km-views/km-letters/km-letter-new-user.html');
$km_email_message = str_replace('%client_first_name%', $kmg_user_firstname, $km_email_message);
$km_email_message = str_replace('%client_last_name%', $kmg_user_lastname, $km_email_message);
$km_email_message = str_replace('%client_username%', $kmg_user_username, $km_email_message);
$km_email_message = str_replace('%client_password%', $km_user_decrypted_password, $km_email_message);
$mpdf->WriteHTML($km_email_message);
$mpdf->Output(dirname(__DIR__, 2).'/km-documents/'.$kmg_zip_directory.'/km-imported-owners-letters/'.$filename,'F');
// array_push($kmg_zip_directory_array, $kmg_zip_directory);
});
$kmg_zip_password = 'pippo';
// Get real path for our folder
$rootPath = realpath(dirname(__DIR__, 2).'/km-documents/router/km-imported-owners-letters');
// Initialize archive object
$zip = new ZipArchive();
$zip->open(dirname(__DIR__, 2).'/km-documents/router/lettere-proprietari-importati.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->setPassword($kmg_zip_password);
// Initialize empty "delete list"
$filesToDelete = array();
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file){
// Skip directories (they would be added automatically)
if (!$file->isDir()){
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file from folder to archive
$zip->addFile($filePath, $relativePath);
// Set up password for the archive
$zip->setEncryptionName($relativePath, ZipArchive::EM_AES_256);
// Add pdf files form directory to array so I can delete them once I've imported them to zip
$filesToDelete[] = $filePath;
}
}
// Zip archive will be created only after closing object
$zip->close();
// Delete all files from "delete list"
foreach ($filesToDelete as $file){
unlink($file);
}
$queue->CloudAMQP_Disconnect();