-
-
Save theStrangeAdventurer/dc784ad237457efb5ea1d33994092cc8 to your computer and use it in GitHub Desktop.
Удаление устаревших файлов по крону, отправка отчета на почту php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
define('MAILTO', 'example@email.com'); | |
define('SUBJECT', 'Отчет по удаленным файлам файлам'); | |
$directories = [ | |
'temp', | |
'upload_files' | |
]; | |
$deleteResult = true; | |
$arrFilesToDelete = []; | |
$filesSizes = 0; | |
// заголовки для отправки e-mail в формате html | |
$headers = 'MIME-Version: 1.0' . "\r\n"; | |
$headers .= 'Content-type: text/html;charset=iso-8859-1' . "\r\n"; | |
$headers .= 'From: noreply@systemmessages.com' . "\r\n" . | |
'Reply-To: example@email.com' . "\r\n" ; | |
foreach ($directories as $dir) { | |
cleanFiles($dir); | |
} | |
if (!$deleteResult) { | |
$mailBody = "Возникли ошибки при удалении файлов, проверьте права доступа"; | |
} else { | |
$mailBody = "<i>Письмо сформировано автоматически</i><br>" . | |
"<p>Были удалены устаревшие файлы <b>(".count($arrFilesToDelete).")</b>" . | |
"<br>Объем освободившихся даных - <b>".formatSizeUnits($filesSizes)."</b>" . | |
"<br>Директории которые были просканированы: <b>".implode(', ', $directories)."</b></p>"; | |
} | |
mail(MAILTO, SUBJECT, $mailBody, $headers); | |
function cleanFiles($dir) | |
{ | |
global $deleteResult, $arrFilesToDelete, $filesSizes; | |
$arrFiles = scandir($dir); | |
$acceptedFiles = []; | |
$expireTime = 3600; | |
$curFilesToDelete = []; | |
foreach ($arrFiles as $file) { | |
$mimeType = mime_content_type($dir . '/' . $file); | |
switch ($mimeType) { | |
case 'image/png': | |
$acceptedFiles[]= $file; | |
break; | |
case 'image/jpeg': | |
$acceptedFiles[]= $file; | |
break; | |
case 'image/gif': | |
$acceptedFiles[]= $file; | |
break; | |
} | |
} | |
foreach ($acceptedFiles as $file) { | |
if (time() - filemtime($dir . '/' . $file) > $expireTime) { | |
$filesSizes += filesize($dir . '/' . $file); | |
$curFilesToDelete[] = $dir . '/' .$file; | |
$arrFilesToDelete[] = $dir . '/' .$file; | |
} | |
} | |
foreach ($curFilesToDelete as $file) { | |
if (!unlink($file)) { | |
$deleteResult = false; | |
} | |
} | |
} | |
function formatSizeUnits($bytes) | |
{ | |
if ($bytes >= 1073741824) | |
{ | |
$bytes = number_format($bytes / 1073741824, 2) . ' GB'; | |
} | |
elseif ($bytes >= 1048576) | |
{ | |
$bytes = number_format($bytes / 1048576, 2) . ' MB'; | |
} | |
elseif ($bytes >= 1024) | |
{ | |
$bytes = number_format($bytes / 1024, 2) . ' KB'; | |
} | |
elseif ($bytes > 1) | |
{ | |
$bytes = $bytes . ' bytes'; | |
} | |
elseif ($bytes == 1) | |
{ | |
$bytes = $bytes . ' byte'; | |
} | |
else | |
{ | |
$bytes = '0 bytes'; | |
} | |
return $bytes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment