<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <form action="<?php $_SERVER["PHP_SELF"] ?>" method="POST" enctype="multipart/formdata"> <p>Выберите файл для загрузки: <br> <input type="file" name="image"> </p> <input type="submit" value="Отправить"> </form> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = $_FILES["image"]["name"]; $temp_name = $_FILES["image"]["tmp_name"]; $size = $_FILES["image"]["size"]; $ext = pathinfo($name, PATHINFO_EXTENSION); $ext = strtolower($ext); if ($ext != "png" && $ext != "jpg" && $ext != "gif") { echo "Изображение должно быть в формате PNG, JPG (JPEG) или GIF.<br>"; exit(); } if ($size > 512000) { echo "Размер файла не должен превышать 512 Кб.<br>"; exit(); } if (file_exists($name)) { echo "Файл с именем $name уже был выгружен.<br>"; exit(); } try { move_uploaded_file($temp_name, $name); echo "Файл $name выгружен:<br>"; echo '<img src= "' . $name . '"'; } catch(Exception $e) { echo "Файл $name не может быть выгружен.<br>"; } } ?> </body> </html> 

I load the image, then I click send and errors are output for the lines of code where the variables $ name, $ tmp_name and $ size are assigned values. Errors such: Notice: Undefined index image

NEW PROBLEM:

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <form action="<?php $_SERVER["PHP_SELF"] ?>" method="POST" enctype="multipart/form-data"> <p>Выберите файл для выгрузки: <br> <input type="file" name="image"> </p> <input type="submit" value="Отправить"> </form> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = $_FILES['image']['name']; $temp_name = $_FILES["image"]["tmp_name"]; $size = $_FILES["image"]["size"]; $ext = pathinfo($name, PATHINFO_EXTENSION); $ext = strtolower($ext); try { if ($ext != "png" && $ext != "jpg" && $ext != "gif") { throw new Exception("Изображение должно быть в формате PNG, JPG (JPEG) или GIF.<br>"); } if ($size > 512000) { throw new Exception("Размер файла не должен превышать 512 Кб.<br>"); } if (file_exists($name)) { throw new Exception("Файл уже был выгружен!<br>"); } move_uploaded_file($temp_name, $name); echo "Файл $name выгружен:<br>"; echo '<img src= " '. $name .' "'; } catch(Exception $e) { echo $e->getMessage(); } } ?> </body> </html> 

The exception is triggered all the time The file has already been unloaded!

  • enctype="multipart/form-data" and enctype="multipart/formdata" ? - Rustam Gimranov
  • @RustamGimranov formdata is written in my textbook. In other sources I saw that it was necessary to write form-data. In both cases, does not work: \ - JustLearn
  • Eh?))) The echo "Изображение должно быть в формате PNG, JPG (JPEG) или GIF.<br>"; line should now work out echo "Изображение должно быть в формате PNG, JPG (JPEG) или GIF.<br>"; - Rustam Gimranov
  • @ RustamGimranov can you please explain in more detail? I do not quite understand what you are saying - JustLearn
  • @RustamGimranov, for some reason, I really work out the line echo "The image must be in PNG, JPG (JPEG) or GIF format. <br>"; BUT: why does this happen if the .jpg file is uploaded? And I get 3 errors notice: undefined index image - JustLearn 5:59 pm

1 answer 1

1 Change enctype="multipart/formdata" to enctype="multipart/form-data" . Information about uploading the file to the server here .

2 Checking the file extension with pathinfo is not safe. Quote from the site www.php.net:

Do not use ... to verify that the file is an image file. For these purposes, use the Fileinfo extension.

On stackoverflow there are many good examples of how to upload a file to the server.

3 The beginning of the try...catch construct can be raised higher:

 ... $size = $_FILES["image"]["size"]; try { if ($size > 512000) { throw new \Exception('Размер файла не должен превышать 512 Кб.'); } ... Тут остальные проверки и загрузка. } catch (\Exception $e) { echo $e->getMessage(); } 
  • Thank you) Now Notice: Undefined index image does not issue - JustLearn
  • But every time I try to download a file, even the one that I have never tried to download at all, it checks that the file has already been downloaded. Please help with this problem - JustLearn
  • ok, now I will update in question - JustLearn 2:07 pm
  • I updated the question - JustLearn
  • I do not know why. Like all the rules. The only thing you can change is: action="<?= $_SERVER["PHP_SELF"] ?>" Instead of this action="<?php $_SERVER["PHP_SELF"] ?>" - Rustam Gimranov