Created
April 11, 2023 07:53
-
-
Save palexandrite/3b91bd9db12e1b5df51319341010e49f to your computer and use it in GitHub Desktop.
Upload files on a server side. Using PHP without any checks and libs.
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Страница с примером передачи переменных с помощью Post</title> | |
</head> | |
<body> | |
<form enctype="multipart/form-data" action="upload.php" method="post"> <!--для сервера--> | |
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" /><!--максимально допустимый размер файла для загрузки в байтах--> | |
Выбрать файл для загрузки: <input name="uploaded_file" type="file" /><!--файл для загрузки--> | |
<input type="submit" value="Отправить" /><!--кнопка загрузки--> | |
</form> | |
</body> | |
</html> |
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 | |
// check if a file is uploading | |
if (!empty($_FILES['uploaded_file']) && ($_FILES['uploaded_file']['error'] == 0)) { | |
$filename = basename($_FILES['uploaded_file']['name']); | |
$extension = substr($filename, strrpos($filename, '.') + 1); | |
if (($extension == 'jpg') && ($_FILES['uploaded_file']['type'] == 'image/jpeg') &&($_FILES['uploaded_file']['size'] < 350000000)) { | |
// a path for saving of a file | |
$newname = dirname(__FILE__) .'/upload/'. $filename; | |
// check existing of a file with a new name | |
if (!file_exists($newname)) { | |
// move a file to a new place | |
if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $newname))) { | |
echo 'Прелестно, файл был загружен: '. $newname; | |
} else { | |
echo 'Произошла ошибка при загрузке файла!'; | |
} | |
} else { | |
echo 'Ошибка: файл'. $_FILES['uploaded_file']['name'] .'уже существует'; | |
} | |
} else { | |
echo 'Ошибка при загрузке файла, изображение не .jpg или больше чем 350кб.'; | |
} | |
} else { | |
echo 'Ошибка: файл не загружен!'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment