How to check file upload area?

I can not verify the correctness of the file upload fields, here is my code:

<?php if (isset($_POST['submit'])) { if ($_POST['uploadFile']) { echo "File uploaded"; } else { echo "No file attempted to be uploaded"; } } ?> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="uploadFile" /> <input type="submit" name="submit" /> </form> 

What mistake am I making?

EDIT

I came up with my own solution, maybe not the best, but it works:

 if (isset($_POST['submit'])) { $fileUpload = $_FILES['uploadFile'] ['name']; if (strlen($fileUpload) > 0) { echo "File uploaded."; } else { echo "No File attempted to be uploaded."; } } 
+4
source share
4 answers

The superglobal you are looking for is $ _FILES.

 var_dump($_FILES); //to see what happening with the uploaded files 

To make a POST request, you can use the following lines:

 if($_SERVER['REQUEST_METHOD'] == 'POST') { //A post request! } 
+2
source

For verification, take a look at is_uploaded_file() and read Handling File is_uploaded_file() .

+2
source

In this script we will add some restrictions on file upload. The user can only upload .gif or .jpeg files, and the file size should be less than 20 kb:

 <?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; } } else { echo "Invalid file"; } ?> 
+1
source

In downloading files with PHP, I think $ _FILES [$ file] ['error'] is your best friend! it breaks it into 8 possible scenarios, 0 means that the file upload went fine, so something like below should do the trick

 if($_FILES[$file]['error']==0) // proceed else switch($_FILES[$file]['error']) // debugging 
0
source

Source: https://habr.com/ru/post/1313975/


All Articles