I want the user to be able to upload photos to the site folder using a button (and for their further output). He writes that he uploaded a photo, although it does not appear in the site folder. What have I done wrong?

<? include '/db.php'; ?> <!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8"> </head> <body> <form enctype="multipart/form-data" method="post" action="/index.php"> <input name="picture" type="file"> <input type="submit" value="Загрузить"> </form> <? $path = '/avatars'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (!@copy($_FILES['picture']['tmp_name'], $path . $_FILES['picture']['name'])) { echo 'Что-то пошло не так'; } else { echo 'Фотография удачно загружена!'; } } ?> </body> </html> 

 Возможная атака с помощью файловой загрузки! Некоторая отладочная информация:Array ( [picture] => Array ( [name] => 4prX6WSofMU.jpg [type] => [tmp_name] => [error] => 2 [size] => 0 ) ) 
  • Remove @ and understand what the error is. - Skrillexazem
  • @Skrillexazem removed and nothing has changed ( - Cool_cool
  • Warning writes ?? - Skrillexazem
  • With this sign, you suppressed the display of errors, which is better not to do if the code stops working. - Skrillexazem

1 answer 1

Try

 <?php if(isset($_POST['go'])){ $uploaddir = 'avatars/'; $uploadfile = $uploaddir . basename($_FILES['picture']['name']); echo '<pre>'; if (move_uploaded_file($_FILES['picture']['tmp_name'], $uploadfile)) { echo "Файл корректен и был успешно загружен.\n"; } else { echo "Возможная атака с помощью файловой загрузки!\n"; } echo 'Некоторая отладочная информация:'; print_r($_FILES); print "</pre>"; } ?> <!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8"> </head> <body> <!-- Тип кодирования данных, enctype, ДОЛЖЕН БЫТЬ указан ИМЕННО так --> <form enctype="multipart/form-data" method="POST"> <!-- Поле MAX_FILE_SIZE должно быть указано до поля загрузки файла --> <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> <!-- Название элемента input определяет имя в массиве $_FILES --> Отправить этот файл: <input name="picture" type="file" /> <input type="submit" value="Отправить файл" name="go"/> </form> </body> 

  • Did as you, look, supplemented the answer - Cool_cool
  • I answered what the reason for the question. - Skrillexazem
  • Earned? (it is necessary to change the value in <input type = "hidden" name = "MAX_FILE_SIZE" value = "30000" />) - Skrillexazem
  • Yes, it worked. thanks - Cool_cool
  • No problem. For the future - php.net/manual/ru/features.file-upload.post-method.php - Skrillexazem