Home / Web Development Technologies / PHP File Upload Step By Step Tutorial

PHP File Upload Step By Step Tutorial

php file upload

PHP File Upload – step by step Tutorial




PHP File Upload – A PHP script can be used with HTML to allow users to upload files to the server. At first documents are transferred into a temporary directory and after that moved to an objective destination by PHP code.
Fist of all lets Make a HTML form that permit users to pick the picture they need to upload:

<!DOCTYPE html>
<html>
<body>

<form action=”upload.php” id=”MyForm” 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>

</body>
</html>

A few rules to take after for the HTML form above:
Ensure that the form utilizes method=”post”
The form require the following attributes too: enctype=”multipart/form-data”. It indicates which content-type to utilize while submitting the form

Another important thing:
The type=”file” property of the <input> tag display the input field as a file-select control, with a “Browse” button alongside the data control.

The above form sends data to a php file called “upload.php”, which we will make next.

PHP File Upload Script.

The “upload.php” file contains the code for uploading a file:

<?php
$target_dir = “uploads_folder/”;
$target_file = $target_dir . basename($_FILES[“fileToUpload”][“name”]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// validate image type
if(isset($_POST[“submit”])) {
$check = getimagesize($_FILES[“fileToUpload”][“tmp_name”]);
if($check !== false) {
echo “Correct file type – ” . $check[“mime”] . “.”;
} else {
echo “incorrect file type.”;
}
}
?>

PHP script clarified:
$target_dir = “uploads_folder/” – determines the folder where the file will be saved
$target_file determines the folder path of the file to be saved
$imageFileType used to check file extension
Next, check if the uploaded file is an actual image or a fake image

If there is any suggestion or query regarding “PHP File Upload” please feedback us in form of comment. Thanks

PHP File Upload - step by step Tutorial PHP File Upload - A PHP script can be used with HTML to allow users to upload files to the server. At first documents are transferred into a temporary directory and after that moved to an objective destination by PHP code. Fist of all lets Make a HTML form that permit users to pick the picture they need to upload: Another important thing: The type="file" property of the <input> tag display the input field as a file-select control, with a "Browse" button alongside the data control. The above form sends data to…

0%

User Rating: 4.25 ( 1 votes)

Check Also

AngularJS and Laravel Blade

How to use AngularJS and Laravel Blade together

How to use AngularJS and Laravel Blade together In this article I will show you …

Ajax Pagination Using Laravel

How to Create an Ajax Pagination Using Laravel

In this article I will demonstrate one way to create an Ajax pagionation using Laravel …

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *