Hello, welcome to zhullyblog. If this is your first time here , please
Subscribe to Zhullyblog by Email to get latest tutorial.
How to upload a file in PHP
In this tutorial , we would learn how to upload a file in PHP using HTML form and PHP. From PHP documentation, we learnt that we can upload image in PHP.
By file, we mean
Images ,
Videos ,
PDFs ,
zip files and other type of file. PHP allows you to upload file into the server.
Facebook was built with PHP and you know that the application allows upload of videos , images and other files.
When building a website , there are times where users want to upload files and save it in the system.
So let’s begin.
The very first thing you need to do is create a form in HTML. The form will ask the user to upload file
This is the code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>File Upload Form</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<h2>Upload File</h2>
<label for="fileSelect">Filename:</label>
<input type="file" name="photo" id="fileSelect">
<input type="submit" name="submit" value="Upload">
<p><strong>Note:</strong> Only .jpg, .jpeg, .gif, .png formats allowed</p>
</form>
</body>
</html>
Screenshot
Output
The next step is process the file you upload.
Don’t forget we have a file as upload.php file. This file “
upload.php” will store the uploaded file in upload folder as well as implement some security check. Now, let’s complete the code.
<?php
// Check if the form was submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Check if file was uploaded without errors
if(isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0){
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
$filename = $_FILES["photo"]["name"];
$filetype = $_FILES["photo"]["type"];
$filesize = $_FILES["photo"]["size"];
// Verify file extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");
// Verify file size
$maxsize = 5 * 1024 * 1024;
if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
if(in_array($filetype, $allowed)){
// Check whether file exists before uploading it
if(file_exists("upload/" . $filename)){
echo $filename . " already exists.";
} else{
move_uploaded_file($_FILES["photo"]["tmp_name"], "upload/" . $filename);
echo "Your file was uploaded successfully.";
}
} else{
echo "Sorry, there was a problem uploading your file. Please try again.";
}
} else{
echo "Error: " . $_FILES["photo"]["error"];
}
}
?>
Screenshot
Okay ,well the code is a little bit creepy but let me break it down.
Once the user or client submit a the file, information about the file such as size can be accessed via PHP superglobal array called
$_FILES. Now, the file we asked the user to submit is an image , so what I am saying is that the information such as name , size or error can be accessed.
Let’s take it but by bit.
•
$_FILES[“image”][“name”]
This will specify the name of the file including the file extension.
•
$_FILES[“image”][“type”]
This specify the MME type of the file.
•
$_FILES[“image”][“size”]
This specify the size in byte. Of course , you should know that the size of whatever information you want to upload must be specific.
•
$_FILES[“image”][“tmp_name]
This specifies the temporary name of the file uploaded to the server.
•
$_FILES[“image”][“error”]
•This specifies error
• The first IF statement is quite explanatory
• The second If says that if the file uploaded does not comply with the condition then it should echo error, please select a valid file. This will only happen when the user select file that is Invalid.
• The third IF checks the size of the file. If the file is larger than the size specified, it will throw an error.
• The fourth checks whether the file exist or not.
So, once the user select the file, there will be an info showing that a file has been selected. Take a look at this picture.
The next step is to store it in a temporary web server once it has been uploaded.
Once a file has been successfully uploaded, it would be stored in a temporary directory on the server.
But you can decide to store it in a permanent directory.
Please share
Follow us on
1) Introduction to PHP
2) PHP Syntax
3) PHP Comment
4) PHP Data types
5) PHP Variables
6) PHP Echo and Print - How to display output
7) PHP If, if else and if elseif statement
8) PHP Lopps - While , Do while
9) PHP Strings : How to create and manipulate Strings
10) PHP Arrays
11)PHP FORMS: How to create a form, access data submitted in the form by user
We are open to any suggestions or ideas on this topic, please drop your comment below
ReplyDelete