WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP 5 File Upload


In this instructional exercise, it is anything but difficult to transfer documents to the server.

However, easily comes peril, so dependably be watchful when permitting document uploads!


Configure The "php.ini" File

First, guarantee that PHP is arranged to permit document uploads.

In your "php.ini" document, look for the file_uploads mandate, and set it to On:

file_uploads = On

Create The HTML Form

Next, make a HTML structure that enable clients to pick the picture document they need to upload:

<!DOCTYPE html>
<html>
<body>

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

</body>
</html>

Some guidelines to pursue for the HTML structure above:

  • Make beyond any doubt that the structure utilizes method="post"
  • The structure additionally needs the accompanying trait: enctype="multipart/structure data". It indicates which content-type to utilize while presenting the form

Without the prerequisites over, the record transfer won't work.

Other things to notice:

  • The type="file" quality of the <input> label demonstrates the info field as a document select control, with a "Browse" catch alongside the information control

The structure above sends information to a record called "upload.php", which we will make next.


Create The Upload File PHP Script

The "upload.php" document contains the code for transferring a file:

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
//Check if picture document is a real picture or phony image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        reverberation "File is a picture - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        reverberation "File isn't an image.";
        $uploadOk = 0;
    }
}
?>

PHP content explained:

  • $target_dir = "uploads/" - indicates the registry where the record will be placed
  • $target_file indicates the way of the record to be uploaded
  • $uploadOk=1 isn't utilized yet (will be utilized later)
  • $imageFileType holds the record augmentation of the file
  • Next, check if the picture record is a genuine picture or a phony image

Check if File Already Exists

Now we can include some restrictions.

First, we will check if the document as of now exists in the "uploads" organizer. On the off chance that it does, a mistake message is shown, and $uploadOk is set to 0:

/Check if record as of now exists
if (file_exists($target_file)) {
    reverberation "Sorry, record as of now exists.";
    $uploadOk = 0;
}

Limit File Size

The record input field in our HTML structure above is named "fileToUpload".

Now, we need to check the extent of the document. In the event that the document is bigger than 500kb, a mistake message is shown, and $uploadOk is set to 0:

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

Limit File Type

The code underneath just enables clients to transfer JPG, JPEG, PNG, and GIF documents. All other record types gives a blunder message before setting $uploadOk to 0:

/Allow certain record formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    reverberation "Sorry, just JPG, JPEG, PNG & GIF records are allowed.";
    $uploadOk = 0;
}

How to Complete Upload File PHP Script

The complete "upload.php" record presently looks like this:

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
//Check if picture record is a genuine picture or phony image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        reverberation "File is a picture - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        reverberation "File isn't an image.";
        $uploadOk = 0;
    }
}
//Check if document as of now exists
if (file_exists($target_file)) {
    reverberation "Sorry, record as of now exists.";
    $uploadOk = 0;
}
/Check document size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    reverberation "Sorry, your document is too large.";
    $uploadOk = 0;
}
//Allow certain record formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    reverberation "Sorry, just JPG, JPEG, PNG & GIF records are allowed.";
    $uploadOk = 0;
}
//Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    reverberation "Sorry, your document was most certainly not uploaded.";
//if all is well, endeavor to transfer file
} else {
    on the off chance that (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        reverberation "The record ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        reverberation "Sorry, there was a mistake transferring your file.";
    }
}
?>