HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/limestate-api/Models/File.php
<?php
/***********************************************************
*
*  БИБЛИОТЕКА РАБОТЫ С ЛОГАМИ
*
************************************************************/

class File
{
    public static function isImage($file)
    {
        if (is_object($file)) {
            return in_array(mb_strtolower($file->type), array('jpg', 'jpeg', 'gif', 'png'));
        } else {
            $path_parts = pathinfo($file);
            if (isset($path_parts['extension'])) {
                return in_array(mb_strtolower($path_parts['extension']), array('jpg', 'jpeg', 'gif', 'png'));
            } else {
                return false;
            }

        }
    }

    public static function load($filename)
    {
        if (file_exists($filename)) {
            $mimetype = mime_content_type($filename);
            header ($_SERVER["SERVER_PROTOCOL"] . ' 200 OK');
            header ('Content-Type: ' . $mimetype);
            // Открываем искомый файл
            $f = fopen ($filename, 'r');
            while (!feof ($f)) {
                // Читаем килобайтный блок, отдаем его в вывод и сбрасываем в буфер
                echo fread ($f, 1024);
                flush ();
            }
            // Закрываем файл
            fclose ($f);
        }  else {
            header ($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
            header ('Status: 404 Not Found');
            echo '404 Not Found';
        }
        exit;
    }

	public static function send($filename, $fileContent, $mimetype = 'application/octet-stream', $isGenerated = false)
	{
		header ($_SERVER["SERVER_PROTOCOL"] . ' 200 OK');
		header ('Content-Type: ' . $mimetype);

		if (!$isGenerated) {
            header('Last-Modified: ' . gmdate('r', filemtime($filename)));
            header('ETag: ' . sprintf('%x-%x-%x', fileinode($filename), filesize($filename), filemtime($filename)));
            header('Content-Length: ' . (filesize($filename)));
        }
		header ('Connection: close');
		header ('Content-Disposition: attachment; filename="' . basename ($filename) . '";');

		echo $fileContent;
		exit;
	}

    public static function download($filename, $mimetype = 'application/octet-stream')
    {
        if (file_exists($filename)) {
            header ($_SERVER["SERVER_PROTOCOL"] . ' 200 OK');
            header ('Content-Type: ' . $mimetype);
            header ('Last-Modified: ' . gmdate ('r', filemtime ($filename)));
            header ('ETag: ' . sprintf ('%x-%x-%x', fileinode ($filename), filesize ($filename), filemtime ($filename)));
            header ('Content-Length: ' . (filesize ($filename)));
            header ('Connection: close');
            header ('Content-Disposition: attachment; filename="' . basename ($filename) . '";');

            // Открываем искомый файл
            $f = fopen ($filename, 'r');
            while (!feof ($f)) {
                // Читаем килобайтный блок, отдаем его в вывод и сбрасываем в буфер
                echo fread ($f, 1024);
                flush ();
            }
            // Закрываем файл
            fclose ($f);
        } else {
            header ($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
            header ('Status: 404 Not Found');
            echo '404 Not Found';
        }
        exit;
    }

    public static function urlToTmp($url) {
        $url = str_replace('https://limestate.ru', 'https://bitrix.limestate.ru', $url);
        $url = str_replace('http://limestate.ru', 'https://bitrix.limestate.ru', $url);
        $content = file_get_contents($url);
        $tmpName = md5(time());
        file_put_contents(ROOT . 'tmp/' . $tmpName, $content);

        return $tmpName;
    }

    public static function clearTmp($tmpName) {
        if (file_exists(ROOT . 'tmp/' . $tmpName)) {
            unlink(ROOT . 'tmp/' . $tmpName);
        }
    }

    public static function generateFilePath($filename)
    {
        $year = date('Y', time());
        $month = date('m', time());
        $day = date('d', time());
        $folder = 'uploads';

        $targetPath = ROOT . $folder . '/';
        $targetPath = str_replace('//', '/', $targetPath);
        $targetPath .= $year . '/' . $month . '/' . $day . '/';

        if (!file_exists($targetPath)) {
            if (!mkdir($targetPath, 0755, true)) {
                throw new Exception('Невозможно создать директорию для загрузки файла: ' . $targetPath);
            }
        }

        $targetFile =  $targetPath . $filename;
        $targetFile = str_replace('//', '/', $targetFile);

        return [
            'path' => $targetFile,
            'url' => substr($targetFile, strlen(ROOT) - 1),
        ];
    }

}