Jul 6 2021
PHP – detecting new uploaded images and sending an e-mail with attachments
Quick example today if a php script that I wanted to detect new uploaded images, and then send an e-mail.
I think it’s pretty self-explanatory so won’t go into details. However, keep in mind that it’s mostly simple PHP (not too object-oriented) and also that I make use of the wp_mail function.
$menPath = "path-to-uploaded-files/*.jpg"; $menLast = file_get_contents('timestampFile.txt'); if($menLast === false){ echo 'it is zero'; $menLast = 0; } $menFiles = glob($menPath); $menFiles = array_filter($menFiles, function ($file) use ($menLast) { $mentest = $menLast - filemtime($file); return filemtime($file) > $menLast; }); file_put_contents('timestampFile.txt', time()); if (count($menFiles) > 0 ) { $menText = "images uploaded\r\nFound images count:".count($menFiles) ."Current last timestamp recorded:".date('d-m-Y h:i:s', $menLast )."\r\n" ."Images found:\r\n"; <code>foreach ($menFiles as &$file) { $menText = $menText.date('d-m-Y h:i:s', filemtime($file))."-".basename($file)."\r\n"; }</code> wp_mail( "yourEmail@gmail.com", "files-uploaded", $menText ,'',$menFiles); } ?>
Recent Comments