Last active
July 20, 2023 14:35
-
-
Save p182/abbf3b37a731f6f6deb2c28f70eb313d to your computer and use it in GitHub Desktop.
Импортирование файла с проверкой на расширение и автоматическим перенаправлением
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
<?php | |
header('Content-Type: text/html; charset=utf-8'); //Заголовок с типом HTML и кодировкой UTF-8 | |
$url = $_GET['url']; //Переменная с адресом загрузки | |
$name = $_GET['name']; //Переменная с именем файла в папке назначения | |
$source = $_GET['source']; | |
if(preg_match('/^.*\.(\S*)$/', $name, $res)){//Выделение расширения (последней части, после точки в имени файла $name) | |
if (in_array($res[1], array("jar", "zip"))){//Проверка, что расширение в списке разрешённых | |
$arrRequestHeaders = array(//установка заголовка GET запроса с referrer и поддержкой перенаправлений | |
'http'=>array( | |
'method' =>'GET', | |
'protocol_version' =>1.1, | |
'follow_location' =>1, | |
'header'=> "User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0\r\n" . | |
"Referer: $source\r\n" | |
) | |
); | |
if (copy($url,$name,stream_context_create($arrRequestHeaders))){//При успешном копировании перенаправляем на файл | |
header("Location: ".$name);} | |
else {//Если не удалось скопировать, то повторяем попытку | |
echo '<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<div class="site-footer push"><font color="blue"> | |
<h2>Скачивание файла началось!</h2><meta http-equiv="Refresh" content="1; URL="></font>'; | |
}} | |
else {//Если расширение не в списке разрешённых, то выводим ошибку и перенаправляем в верхнюю директорию | |
echo '<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<div class="site-footer push"><font color="red"> | |
<h2>Только файлы .jar и .zip!</h2><meta http-equiv="Refresh" content="1; URL=../"></font>';}} | |
else {//Если имя файла не содержит точку, то выводим ошибку и перенаправляем в верхнюю директорию | |
echo '<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<div class="site-footer push"><font color="red"> | |
<h2>Некорректное имя файла!</h2><meta http-equiv="Refresh" content="1; URL=../"></font>';} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment