📖 Scenario: You are building a simple web page where users can upload a profile picture. You want to learn how PHP handles uploaded files using the $_FILES superglobal.
🎯 Goal: Create a PHP script that accepts a file upload from an HTML form, accesses the uploaded file information using $_FILES, and displays the original file name and file size.
📋 What You'll Learn
Create an HTML form with a file input named profile_pic and method POST with enctype="multipart/form-data".
Create a PHP script that accesses the uploaded file data using $_FILES['profile_pic'].
Extract the original file name and file size from $_FILES['profile_pic'].
Display the file name and size in bytes on the page.
💡 Why This Matters
🌍 Real World
File uploads are common on websites for profile pictures, documents, or other media. Understanding <code>$_FILES</code> helps you handle these uploads safely and effectively.
💼 Career
Many web developer jobs require handling file uploads securely. Knowing how to use <code>$_FILES</code> is essential for backend PHP development.
Progress0 / 4 steps
1
Create the HTML form for file upload
Write the HTML code to create a form with method="POST" and enctype="multipart/form-data". Inside the form, add a file input with the name profile_pic and a submit button labeled Upload.
PHP
Hint
Remember to set enctype="multipart/form-data" on the form to allow file uploads.
2
Check if a file was uploaded
In PHP, create an if statement that checks if $_FILES['profile_pic']['error'] is equal to UPLOAD_ERR_OK to confirm a file was uploaded without errors.
PHP
Hint
Use isset($_FILES['profile_pic']) to check if the file input exists and compare the error code to UPLOAD_ERR_OK.
3
Extract the uploaded file's name and size
Inside the if block, create two variables: $fileName to store $_FILES['profile_pic']['name'] and $fileSize to store $_FILES['profile_pic']['size'].
PHP
Hint
Use the name and size keys inside $_FILES['profile_pic'] to get the original file name and size in bytes.
4
Display the uploaded file's name and size
Add echo statements inside the if block to print the file name and file size in bytes in this format: File name: [file name] and File size: [file size] bytes.
PHP
Hint
Use echo to print the file name and size with a line break between them.
Practice
(1/5)
1. What does the $_FILES superglobal in PHP store?
easy
A. Database connection details
B. User session data
C. Information about files uploaded via an HTML form
D. Server environment variables
Solution
Step 1: Understand the purpose of $_FILES
$_FILES is a special PHP variable that holds details about files uploaded through a form.
Step 2: Compare with other options
User session data is stored in $_SESSION, database details are not stored here, and server variables are in $_SERVER.
Final Answer:
Information about files uploaded via an HTML form -> Option C
Quick Check:
$_FILES = Uploaded file info [OK]
Hint: Remember: $_FILES always relates to uploaded files [OK]
Common Mistakes:
Confusing $_FILES with $_SESSION
Thinking $_FILES holds server info
Assuming $_FILES stores form text inputs
2. Which of the following is the correct way to access the name of an uploaded file with input name userfile?
easy
A. $_FILES['userfile']['filename']
B. $_FILES['userfile']['file']
C. $_FILES['userfile']['file_name']
D. $_FILES['userfile']['name']
Solution
Step 1: Recall the structure of $_FILES
Each uploaded file's info is stored in an array with keys like 'name', 'type', 'tmp_name', 'error', and 'size'.
Step 2: Identify the correct key for the original filename
The key for the original filename is 'name', not 'filename' or 'file_name'.
Final Answer:
$_FILES['userfile']['name'] -> Option D
Quick Check:
Filename key = 'name' [OK]
Hint: Use 'name' key to get original filename from $_FILES [OK]
Common Mistakes:
Using 'filename' or 'file_name' instead of 'name'
Confusing keys with other $_FILES elements
Trying to access $_POST for file names
3. What will the following PHP code output if a file named 'photo.jpg' is uploaded successfully?
B. Using 'name' instead of 'tmp_name' in move_uploaded_file
C. Checking error with == instead of ===
D. Missing semicolon after echo statement
Solution
Step 1: Understand move_uploaded_file parameters
The first parameter must be the temporary file path stored in tmp_name, not the original filename.
Step 2: Identify the incorrect usage
The code incorrectly uses $_FILES['upload']['name'] as the source file, which is wrong and causes failure.
Final Answer:
Using 'name' instead of 'tmp_name' in move_uploaded_file -> Option B
Quick Check:
move_uploaded_file needs tmp_name as source [OK]
Hint: Use 'tmp_name' as source file in move_uploaded_file [OK]
Common Mistakes:
Using original filename instead of temp file path
Ignoring error codes before moving files
Not validating file size or type
5. You want to save an uploaded file only if it is less than 2MB and has no errors. Which code snippet correctly checks these conditions before moving the file?
if (/* condition here */) {
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
echo 'Upload successful';
} else {
echo 'Upload failed';
}
hard
A. if ($_FILES['file']['error'] === 0 && $_FILES['file']['size'] < 2097152)
B. if ($_FILES['file']['error'] == 1 || $_FILES['file']['size'] > 2097152)
C. if ($_FILES['file']['error'] !== 0 && $_FILES['file']['size'] <= 2097152)
D. if ($_FILES['file']['error'] === 0 || $_FILES['file']['size'] > 2097152)
Solution
Step 1: Check for no upload errors
The error code must be exactly 0 to confirm no errors during upload.
Step 2: Check file size limit
The file size must be less than 2MB (2 * 1024 * 1024 = 2097152 bytes).
Step 3: Combine conditions correctly
Both conditions must be true, so use logical AND (&&) with strict equality and less-than size check.
Final Answer:
if ($_FILES['file']['error'] === 0 && $_FILES['file']['size'] < 2097152) -> Option A
Quick Check:
Error 0 and size < 2MB = correct check [OK]
Hint: Use error === 0 and size < 2MB with && [OK]
Common Mistakes:
Using || instead of && for conditions
Checking error != 0 instead of === 0
Confusing size units or limits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.