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);
 }
 ?>

Menelaos Bakopoulos

Mr. Menelaos Bakopoulos is currently pursuing his PhD both at Center for TeleInFrastruktur (CTiF) at Aalborg University (AAU) in Denmark and Athens Information Technology (AIT) in Athens, Greece. He received a Master in Information Technology and Telecommunications Systems from Athens Information Technology and a B.Sc. in Computer Science & Management Information Systems from the American College of Thessaloniki. Since April 2008 he has been a member of the Multimedia, Knowledge, and Web Technologies Group.

More Posts