Joywork/cron/clearObjectFilesExistInSelectel.php

89 lines
2.1 KiB
PHP
Raw Normal View History

2026-05-22 20:21:54 +02:00
<?php
set_time_limit(0);
ini_set('display_errors', 1);
error_reporting(E_ALL);
$dirPath = $_SERVER['DOCUMENT_ROOT']."/upload/objects";
$sum = 0;
for($num = 85000000; $num <= 96000000; $num++) {
$tempDir = $dirPath . "/" . $num;
if (is_dir($tempDir)) {
clearReqDir($tempDir, $sum);
}
}
echo "<br/>".formatSizeUnits($sum);
function clearReqDir($dirPath, &$sum) {
if (!is_dir($dirPath)) {
throw new InvalidArgumentException("$dirPath must be a directory");
}
if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
$dirPath .= '/';
}
$files = glob($dirPath . '*', GLOB_MARK);
foreach ($files as $file) {
if (is_dir($file)) {
clearReqDir($file, $sum);
} else {
$url = str_replace("/var/www/sites/joywork.ru/prod", "https://uf.joywork.ru", $file);
echo "check url " . $url . "<br/>";
$res = doesFileUrlExists($url);
if ($res[0]) {
$size = filesize($file);
$sum = $sum + $size;
echo "delete " . $file . " " . date("d.m.Y H:i:s.", filemtime($file)) . " size: " . formatSizeUnits($size) . "<br/>";
//если файл есть в селектел, удаляем локальный файл
unlink($file);
}
}
}
}
function doesFileUrlExists($url) {
$headers = get_headers($url, true);
return [(stripos($headers[0], "200 OK") ? true : false), (isset($headers['Content-Length']) ? (int) $headers['Content-Length'] : 0)];
}
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;
}