Skip to content

Instantly share code, notes, and snippets.

@dev-4458
Created December 15, 2021 01:24
Show Gist options
  • Save dev-4458/9f7ff6c4271cd239c22dec2e060920ea to your computer and use it in GitHub Desktop.
Save dev-4458/9f7ff6c4271cd239c22dec2e060920ea to your computer and use it in GitHub Desktop.
PHP script that will scan all the PDF files from particular directory and make a zip, un-link individual PDF file as well
<?php
//CRON : `/usr/local/bin/ea-php74 /var/www/public_html/scan_pdf_and_make_zip.php 2>&1 | tee /var/www/public_html/scan_pdf.log
$zip = new ZipArchive;
$zip->open('/var/www/public_html/zipHavingPDFs.zip', ZipArchive::CREATE);
$targetFolder = "/var/www/public_html/var/*";
if(file_exists('/var/www/public_html/zipHavingPDFs.zip')) {
echo 'zipHavingPDFs file is exiss'.PHP_EOL;
return;
}
foreach (glob($targetFolder) as $file) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ($ext === 'pdf'){
//Remove below comment if you want to remove single PDF file
//$flag = unlink($file);
file_put_contents('/var/www/public_html/var/log/fd.log', $file.'==='.$flag.PHP_EOL , FILE_APPEND | LOCK_EX);
$zip->addFile($file);
}
}
$zip->close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment