Skip to content Skip to sidebar Skip to footer

Does Input Type=files Appear In $_POST?

I have this within a form and I was thinking that as its an input name I should be able to detect it with $_POST[] but I cant see it. Which would explain when I do isset on it not

Solution 1:

You can access posted file data in $_FILES

You can get file name, file type, tmp_name, error, size in $_FILES

Simple exmaple is:

Html:

<form action="upload_manager.php" method="post" enctype="multipart/form-data">
    <h2>Upload File</h2>
    <label for="fileSelect">Filename:</label>
    <input type="file" name="photo" id="fileSelect"><br>
    <input type="submit" name="submit" value="Upload">
</form>

In php:

<?php
if($_FILES["photo"]["error"] > 0){
    echo "Error: " . $_FILES["photo"]["error"] . "<br>";
} else{
    echo "File Name: " . $_FILES["photo"]["name"] . "<br>";
    echo "File Type: " . $_FILES["photo"]["type"] . "<br>";
    echo "File Size: " . ($_FILES["photo"]["size"] / 1024) . " KB<br>";
    echo "Stored in: " . $_FILES["photo"]["tmp_name"];
}
?>

Solution 2:

Files are stored in $_FILES, not $_POST

$_FILES variable

Manual on PHP File Uploads.

html form:

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

upload.php

<?php
$target_dir    = "uploads/";
$target_file   = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk      = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if (isset($_POST["submit"]))
{
      $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
      if ($check !== false)
      {
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
      }
      else
      {
            echo "File is not an image.";
            $uploadOk = 0;
      }
}
?>

Post a Comment for "Does Input Type=files Appear In $_POST?"