PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
48: Building a File Upload and Gallery Script
Think of a file upload script like a high-security mailroom. When a package arrives, the mail clerk doesn't just take the box and put it directly on the CEO's desk. First, they check the label to see what it is. Then they weigh it to make sure it's not a 500-pound anvil. They might even X-ray it to ensure there isn't anything dangerous inside. Only after it passes these checks does the clerk move the package from the temporary loading dock to a specific, organized shelf in the warehouse.
In PHP, the "loading dock" is your server's temporary directory. The "checks" are your validation logic, and the "warehouse shelf" is the final folder where you'll store the images for your gallery. If you skip the mailroom process and just trust the package, you're essentially letting strangers put whatever they want onto your server—which is a great way to get hacked.
Wrestling with the $_FILES Array
Unlike a standard text input, files aren't sent via $_POST. They live in their own superglobal: $_FILES. But before you even touch that array, you have to make sure your HTML form has the correct attribute: enctype="multipart/form-data". I can't tell you how many times I've spent an hour debugging a "missing file" issue only to realize I forgot that one line of HTML. Without it, PHP will never see the file.
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="gallery_image">
<button type="submit">Upload</button>
</form>
When the form submits, $_FILES['gallery_image'] becomes an array containing the original name, the temporary path on the server, the error code, and the file size. We care most about tmp_name (where it is now) and name (what it was called on the user's computer).
Don't Let Users Break Your Server
Here is where we act like the skeptical mail clerk. You should never trust the name provided by the user. A malicious user could upload a file named shell.php, and if you move that into your web root, they can execute code on your server. I always recommend renaming the file to something random or using a timestamp.
We also need to check the MIME type. Don't just trust the file extension; use finfo_file or mime_content_type to verify it's actually an image. I usually limit uploads to a few megabytes because I've seen a single 100MB "profile picture" crash a small shared hosting account.
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
$fileType = mime_content_type($_FILES['gallery_image']['tmp_name']);
if (!in_array($fileType, $allowedTypes)) {
die("Nice try, but we only accept images here.");
}
$uploadDir = 'uploads/';
$fileName = time() . '_' . basename($_FILES['gallery_image']['name']);
$targetPath = $uploadDir . $fileName;
if (move_uploaded_file($_FILES['gallery_image']['tmp_name'], $targetPath)) {
echo "File successfully moved to the warehouse!";
}
Turning a Folder into a Visual Gallery
Now that we have files piling up in our uploads/ folder, we need a way to show them. Instead of manually tracking every file in a database (which you should do for a professional app), we can use glob() for a quick-and-dirty gallery. It's a powerful function that finds pathnames matching a specific pattern.
I like using a simple foreach loop to wrap these results in HTML image tags. To keep the page from looking like a chaotic mess, I usually wrap the output in a CSS grid. It's a clean way to transform a directory of files into a curated visual experience.
$images = glob("uploads/*.{jpg,jpeg,png,gif}", GLOB_BRACE);
echo '<div class="gallery-grid">';
foreach ($images as $image) {
echo "<img src='$image' alt='Gallery Image' style='width:200px; margin:10px;'>";
}
echo '</div>';📋 Practical Task
Build a "Community Travel Photo Archive"
Your task is to create a functional image upload and gallery system. Your project must include:
- An HTML form that allows a user to select an image file.
- A PHP processing script that:
- Validates that the file is actually an image (JPEG, PNG, or GIF).
- Rejects any file larger than 2MB.
- Renames the file using
uniqid()to prevent filename collisions. - Moves the file into a folder named
archive/.
- A display section below the form that automatically scans the
archive/folder and displays all uploaded images in a simple grid of thumbnails.
There are no comments for now.