/** * Загрузка файлов по урл * * $url - адрес файла * $options - массив из опций заливки * -- [file_code]: уникальный код для системы медиаданых * -- [allow_types]: досупные для заливки типы файлов. Например image (тип форматов из таблицы типов файлов file_type_ext) */ public function uploadFromUrl($url, $options = []) { $sitePath = App::$primary->path; $fileinfo = pathinfo($url); if (empty($fileinfo['extension'])) { return 0; } $type = Files\Type::get($fileinfo['extension'], 'ext'); if (!$type) { return 0; } if (!empty($options['accept_group']) && $options['accept_group'] != $type->group) { return 0; } $fileObject = new Files\File(); if (!empty($options['file_code'])) { $fileObject = Files\File::get($options['file_code'], 'code'); if (!$fileObject) { $fileObject = new Files\File(); $fileObject->code = $options['file_code']; } } $fileObject->name = $fileinfo['filename']; $fileObject->path = $type->type_dir . date('Y-m-d') . '/' . microtime(true) . '.' . $fileinfo['extension']; if ($fileObject->id && file_exists($sitePath . $fileObject->path)) { unlink($sitePath . $fileObject->path); } Tools::createDir($sitePath . $type->type_dir . date('Y-m-d') . '/'); $file = @file_get_contents($url); if ($file === false) { return 0; } file_put_contents($sitePath . $fileObject->path, $file); $fileObject->type_id = $type->pk(); $fileObject->original_name = $fileinfo['basename']; $fileObject->upload_code = !empty($options['upload_code']) ? $options['upload_code'] : 'untracked'; $fileObject->save(); return $fileObject->id; }
public function giveFile($file) { if (!file_exists($file) && file_exists(mb_convert_encoding($file, 'Windows-1251', 'UTF-8'))) { $file = mb_convert_encoding($file, 'Windows-1251', 'UTF-8'); } if (!file_exists($file)) { header('HTTP/1.1 404 Not Found'); exit; } $fileinfo = pathinfo($file); if (empty($fileinfo['extension'])) { header('HTTP/1.1 404 Not Found'); exit; } if (!empty($_GET['resize'])) { $allow_resize = false; if (App::$cur->db->connect) { $type = Files\Type::get($fileinfo['extension'], 'ext'); $allow_resize = $type->allow_resize; } if (!$type && in_array(strtolower($fileinfo['extension']), array('png', 'jpg', 'jpeg', 'gif'))) { $allow_resize = true; } if ($allow_resize) { $sizes = explode('x', $_GET['resize']); $sizes[0] = intval($sizes[0]); if (isset($sizes[1])) { $sizes[1] = intval($sizes[1]); } else { $sizes[1] = 0; } if (!$sizes[0] || !$sizes[1]) { header('HTTP/1.1 404 Not Found'); exit; } elseif ($sizes[0] > 2000 || $sizes[1] > 2000) { header('HTTP/1.1 404 Not Found'); exit; } else { $dir = App::$primary->path; if (!empty($_GET['resize_crop'])) { if (in_array($_GET['resize_crop'], array('q', 'c'))) { $crop = $_GET['resize_crop']; } else { $crop = 'c'; } } elseif (!empty($_GET['resize_quadro'])) { $crop = 'q'; } else { $crop = ''; } $pos = 'center'; if (!empty($_GET['resize_pos']) && in_array($_GET['resize_pos'], array('top', 'center'))) { $pos = $_GET['resize_pos']; } $dirnoslash = str_replace('/', '', substr($fileinfo['dirname'], strpos($fileinfo['dirname'], '/static'))); $path = $dir . '/static/cache/' . $dirnoslash . $fileinfo['filename'] . '.' . $sizes[0] . 'x' . $sizes[1] . $crop . $pos . '.' . $fileinfo['extension']; if (!file_exists($path)) { Tools::createDir($dir . '/static/cache/'); copy($file, $path); Tools::resizeImage($path, $sizes[0], $sizes[1], $crop, $pos); } $file = $path; } } } $request = getallheaders(); if (isset($request['If-Modified-Since'])) { // Разделяем If-Modified-Since (Netscape < v6 отдаёт их неправильно) $modifiedSince = explode(';', $request['If-Modified-Since']); // Преобразуем запрос клиента If-Modified-Since в таймштамп $modifiedSince = strtotime($modifiedSince[0]); } else { // Устанавливаем время модификации в ноль $modifiedSince = 0; } header("Cache-control: public"); header("Accept-Ranges: bytes"); header("Pragma: public"); header("Content-Length: " . filesize($file)); header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($file)) . ' GMT'); header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 60 * 60 * 24 * 256) . ' GMT'); if (filemtime($file) <= $modifiedSince && (!isset($_SERVER['HTTP_CACHE_CONTROL']) || $_SERVER['HTTP_CACHE_CONTROL'] != 'no-cache')) { // Разгружаем канал передачи данных! header('HTTP/1.1 304 Not Modified'); exit; } //if( strpos( $file, '/static/doc' ) !== false ) { header('Content-Description: File Transfer'); //} if (isset($this->mimes[strtolower($fileinfo['extension'])])) { header("Content-Type: " . $this->mimes[strtolower($fileinfo['extension'])]); } if (isset($_GET['frustrate_dl']) || in_array($fileinfo['extension'], array('doc', 'docx', 'xls', 'xlsx'))) { $fileName = $fileinfo['filename'] . '.' . $fileinfo['extension']; if (App::$cur->db->connect) { $fileObj = Files\File::get(['path', '%/' . $fileinfo['filename'] . '.' . $fileinfo['extension'], 'LIKE']); if ($fileObj) { $fileName = $fileObj->original_name; } } header('Content-Disposition: attachment; filename="' . $fileName . '"'); } header('Content-Transfer-Encoding: binary'); //} readfile($file); exit; }
<?php return function ($step = NULL, $params = []) { $types = [['dir' => '/static/mediafiles/images/', 'ext' => 'png', 'group' => 'image', 'allow_resize' => 1], ['dir' => '/static/mediafiles/images/', 'ext' => 'jpeg', 'group' => 'image', 'allow_resize' => 1], ['dir' => '/static/mediafiles/images/', 'ext' => 'jpg', 'group' => 'image', 'allow_resize' => 1], ['dir' => '/static/mediafiles/images/', 'ext' => 'gif', 'group' => 'image', 'allow_resize' => 1]]; foreach ($types as $type) { $typeObject = new \Files\Type($type); $typeObject->save(); } };