PHP File Upload Introduction πŸ“πŸ”„

beginner
12 min

PHP File Upload Introduction πŸ“πŸ”„

Welcome to our PHP File Upload tutorial! Today, we're going to dive into one of the most common yet essential tasks in web development: handling file uploads. By the end of this tutorial, you'll be able to create secure, practical, and user-friendly file upload systems. Let's get started! πŸš€

Why do we need to upload files? πŸ“

In many web applications, the ability to upload files is crucial. This could be anything from user profiles, blog posts, or even user-generated content. To create engaging and interactive websites, we need to understand and implement file upload functionality.

Understanding PHP's built-in file upload functionality πŸ’‘

PHP provides a simple yet powerful solution for handling file uploads. The $_FILES superglobal array stores all the information related to the uploaded files. Let's take a closer look at its structure:

array( 'name' => 'filename.ext', 'type' => 'mime-type', 'tmp_name' => '/tmp/php87489', 'error' => 0, 'size' => 12345 )

We'll explore each of these properties in detail throughout this tutorial.

Creating our first file upload form 🎯

Now that we understand the basics of PHP's file upload functionality, let's create a simple file upload form.

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>File Upload Form</title> </head> <body> <h1>File Upload Form</h1> <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload File" name="submit"> </form> </body> </html>

Save the above HTML code as index.php. Now, let's create the PHP script that processes the uploaded file.

php
<?php // Your PHP script goes here ?>

Save the empty PHP file as upload.php. Now, let's write the PHP script that processes the uploaded file.

Processing the uploaded file πŸ“

php
<?php if(isset($_POST['submit'])) { $file = $_FILES['fileToUpload']; $fileName = basename($file['name']); $fileTmpName = $file['tmp_name']; $fileSize = $file['size']; $fileType = $file['type']; $fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); $allowed = array('jpg', 'jpeg', 'png', 'pdf'); if(in_array($fileExt, $allowed)) { if($fileSize < 1000000) { $uploadFileDir = '../uploads/'; $dest_file = $uploadFileDir . $fileName; move_uploaded_file($fileTmpName, $dest_file); echo "The file ". basename($dest_file) . " has been uploaded."; } else { echo "Sorry, your file is too large."; } } else { echo "Sorry, only JPG, JPEG, PNG & PDF files are allowed."; } } ?>

Save the above PHP code in the upload.php file. Now, when you run the index.php file in your web browser, you should see a simple file upload form. Upload a file, and you'll see the success message if the file is valid and within the size limit.

Quick Quiz
Question 1 of 1

Which `$_FILES` property holds the temporary file name of the uploaded file?

In the next part of this tutorial, we'll dive deeper into validating and securing our file uploads. Stay tuned! πŸ”

Happy coding! πŸ’»βœ¨