Description
A developer's journey through code. I build, I break, and I write about it. Explore articles on modern software development, programming tips, and more.
This project may seem straightforward, yet it holds significant importance for every PHP developer. Frequently, developers encounter the need for a file upload system in various projects, such as Content Management Systems (CMS), e-commerce scripts, e-learning scripts, and more. Providing users with the capability to upload various file types which may include videos, images, or audio files in systems that requires it is importent. In this article we will explore the creation of a fundamental yet robust file upload system using PHP. Understanding the ins and outs of file uploads is crucial for web developers as I earlier pointed out, and by the end of this guide, you will have a solid foundation for implementing this feature in your projects.
First create a PHP document which you can assign a name of your choice, here I will call mine "upload.php". Secondly, create a folder to store the uploaded files, you can assign any name to your folder, but I will call mine "uploads". In your PHP document introduce the structure of HTML and a simple form with the input type of file and a submit button, assign a name to the input filed and your submit button. Make sure to recognize those names while writing your PHP code, I have assigned my input field the name "file" and my submit button the name "submit" as you will find in my form element in this article.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Upload files</title>
</head>
<body>
This standard HTML setup includes the necessary meta tags and sets the document title to "Upload files."
<?php
if (isset($_POST['submit'])) {
$file = $_FILES['file'];
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'txt', 'pdf', 'mp4', '3gp');
Now, let us look into the PHP code and study it. The if (isset($_POST['submit'])) condition ensures that the file upload logic is executed only when the form is submitted.
$file = $_FILES['file'];: This line fetches the file details from the submitted form.
Extracting file details:
Extracting file extension:
Defining allowed file types:
if (in_array($fileActualExt, $allowed)) {
if ($fileError === 0) {
if ($fileSize < 1000000) {
$fileNameNew = uniqid('', true).".".$fileActualExt;
$fileDestination = 'uploads/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
echo "File uploaded successfully";
} else {
echo "File size too large";
}
} else {
echo "Error uploading file";
}
} else {
echo "File type not supported!";
}
}
?>
Now, let's break down the core logic:
Checking file type:
Handling file errors:
Checking file size:
Uploading the file:
Displaying messages:
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit" name="submit">Upload</button>
</form>
</body>
</html>
Finally, the HTML form is simple and I used the enctype="multipart/form-data" attribute, which is crucial for file uploads and you should include it while practicing. I also included a file input and a submit button as I earlier stated. If you download the source code below do well to unzip it because I have provided it in a zip file.
I have just walked you through the intricacies of creating a basic file upload system in PHP. This guide provides a detailed breakdown of the code, ensuring that you not only implement the functionality but also understand each component. Feel free to adapt and expand upon this foundation to meet the specific requirements of your projects. You can share your contributions in the comments section below.
Cookies improve user experience on SunshineIHCTS. By continuing to use this website, you consent to the use of cookies in accordance with the Privacy policy.
A developer's journey through code. I build, I break, and I write about it. Explore articles on modern software development, programming tips, and more.
Comments section
You need to be logged in to comment, Login or Register.Approved comments:
No comments yet! be the first to comment