Nov
8

File upload with PHP

11/08/2022 12:00 AM by Publisher in Web_development


First, ensure that PHP is configured to allow file uploads.

In your "php.ini" file, search for the file_uploads directive, and set it to On if it's set to OFF:
file_uploads = On
Create The HTML form that allow users to choose the image file they want to upload:

<html>
<body>

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

</body>
</html>

Some rules to follow for the HTML form above:

    Make sure that the form uses method="post"
    The form needs the attribute: enctype="multipart/form-data". It specifies which content-type to use when submitting the form

Without the requirements above, the file upload will not work.

Other things to notice:

    The type="file" attribute into the <input> tag shows the input field as a file-select control, with a "Browse" button next to it

The form above sends data to a file called "upload.php", which we will create next.

Next we create the php upload file "upload.php"

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// You'll need to create a new directory called "upload" in the directory where "upload.php" file resides. It will be the folder where your files will be saved.
$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["file_to_upload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if the image file is a actual image or not
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["file_to_upload"]["tmp_name"]);
  if($check !== false) {
    echo "The file is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    echo "The file is not an image.";
    $uploadOk = 0;
  }
}

// Check if the file you want to upload already exists
if (file_exists($target_file)) {
  echo "Sorry, the file you want to uplaod already exists.";
  $uploadOk = 0;
}

// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}
?>

PHP script explained:

    $target_dir = "upload/" - specifies the directory where the file is going to be placed
    $target_file specifies the path of the file to be uploaded
    $uploadOk=1 is not used yet (will be used later)
    $imageFileType holds the file extension of the file (in lower case)
    Next, check if the image file is an actual image or not


leave a comment
Please post your comments here.