It is necessary to delete files comparing with the current date.

For example, the following files are created in the folder:

2016-09-01_20-47-22.jpg 2016-09-01_19-42-02.gif 2016-08-23_20-37-42.png 2016-08-17_20-17-22.jpg ΠΈ Ρ‚. Π΄. 

You need to check and delete all files where the date is less than the current one.

Thank.

  • write all names to the array, then in the chain of each element of the array (file name), take the first 10 characters and match it with the current date if it does not match, delete the file. It's simple. - Dmitriy Kondratiuk

2 answers 2

 <?php $dir = "img/1/"; // ΠΏΠ°ΠΏΠΊΠ° Π³Π΄Π΅ Π΅ΡΡ‚ΡŒ Ρ„Π°ΠΉΠ»Ρ‹ c изобраТСниями $img_a = array(); // пустой массив $dateNow = date("Ymd"); // Π΄Π°Ρ‚Π° Π½Π° ΠΌΠΎΠΌΠ΅Π½Ρ‚ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠΈ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ "2016-09-02" if (is_dir($dir)){ // провСряСм ΠΈΠ»ΠΈ ΡƒΠΊΠ°Π·Π°Π½ΠΎ ΠΏΠ°ΠΏΠΊΡƒ if($od = opendir($dir)){ //ΠΎΡ‚ΠΊΡ€Ρ‹Π²Π°Π΅ΠΌ ΠΏΠ°ΠΏΠΊΡƒ while(($file = readdir($od)) !== false){ // провСряСм всС Ρ„Π°ΠΉΠ»Ρ‹ Π² ΠΏΠ°ΠΏΠΊΠ΅ if(strtolower(strstr($file, "."))===".jpg" || strtolower(strstr($file, "."))===".gif" || strtolower(strstr($file, "."))===".png"){ // провСряСм ΠΈΠ»ΠΈ Ρ„Π°ΠΉΠ»Ρ‹ ΠΈΠΌΠ΅ΡŽΡ‚ Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΡ .jpg, .gif, .png array_push($img_a, $file); // добавляСм ΠΈΠΌΠ΅Π½Π° Π² массив } } closedir($od); //Π·Π°ΠΊΡ€ΠΈΠ²Π°Π΅ΠΌ ΠΏΠ°ΠΏΠΊΡƒ } } for($i=0;$i<count($img_a);++$i){ //запускаСм Ρ†Ρ‹ΠΊΠ» ΠΏΠ΅Ρ€Π΅Π±ΠΎΡ€Π° масива $fileDate = substr($img_a[$i], 0, 10); //Π±Π΅Ρ€Π΅ΠΌ ΠΏΠ΅Ρ€Π²Ρ‹Π΅ 10 символов ΠΈΠΌΠ΅Π½ΠΈ if($fileDate != $dateNow){ // провСряСм Ссли Π΄Π°Ρ‚Π° Ρ„Π°ΠΉΠ»Π° Π½Π΅ соотвСтствуСт Π΄Π°Ρ‚Π΅ Π½Π° ΠΌΠΎΠΌΠ΅Π½Ρ‚ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠΈ Ρ‚ΠΎ echo 'ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ '.$img_a[$i].' Π½Π΅ ΠΈΠΌΠ΅Π΅Ρ‚ Ρ‚Π΅ΠΊΡƒΡ‰ΡƒΡŽ Π΄Π°Ρ‚Ρƒ <br/>'; //ΡƒΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ какая ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ° Π½Π΅ ΠΈΠΌΠ΅Π΅Ρ‚ Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ Π΄Π°Ρ‚Ρ‹ unlink($dir.$img_a[$i]); // удаляСм ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΡƒ с ΠΏΠ°ΠΏΠΊΠΈ } else{ echo 'ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ '.$img_a[$i].' ΠΈΠΌΠ΅Π΅Ρ‚ Ρ‚Π΅ΠΊΡƒΡ‰ΡƒΡŽ Π΄Π°Ρ‚Ρƒ <br/>'; //ΡƒΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ какая ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ° ΠΈΠΌΠ΅Π΅Ρ‚ Ρ‚Π΅ΠΊΡƒΡ‰ΡƒΡŽ Π΄Π°Ρ‚Ρƒ } } ?> 

    You can start from the following script

     <?php $arr[] = '2016-09-02_20-47-22.jpg'; $arr[] = '2016-09-02_19-42-02.gif'; $arr[] = '2016-08-23_20-37-42.png'; $arr[] = '2016-08-17_20-17-22.jpg'; foreach($arr as $filename) { $pattern = '/^(\d{4})-(\d{2})-(\d{2})_(\d{2})-(\d{2})-(\d{2})/'; if(preg_match($pattern, $filename, $out)) { if(mktime($out[4], $out[5], $out[6], $out[2], $out[3], $out[1]) < time()) { unlink($filename); } } } 

    Instead of the $arr array and the foreach() you can use the glob() function as a source for the file list source, for example, using the glob() function.

     <?php foreach (glob("*") as $filename) { $pattern = '/^(\d{4})-(\d{2})-(\d{2})_(\d{2})-(\d{2})-(\d{2})/'; if(preg_match($pattern, $filename, $out)) { if(mktime($out[4], $out[5], $out[6], $out[2], $out[3], $out[1]) < time()) { unlink($filename); } } } 

    If the time in the file should not be taken into account, then instead of the date and time

     mktime($out[4], $out[5], $out[6], $out[2], $out[3], $out[1]) 

    you can only generate the date by zeroing the first three parameters of the mktime() function

     mktime(0, 0, 0, $out[2], $out[3], $out[1])