Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 42

PHP Image Upload (Secure Full Code)

In this lesson: Upload images in PHP safely — type and size checks, unique names, move_uploaded_file and preview — full code.

What we are building

An image upload lets users send a picture — a profile photo, a product image — to your server. It has two parts: an HTML form that sends the file, and PHP that receives, checks and saves it. Doing the checks properly is important, because uploads are a common security weak point.

💡 Uploaded files arrive in the special $_FILES array, with a temporary path you then move to a real folder.

The upload form

<form method="post" action="upload.php"
      enctype="multipart/form-data">
  <input type="file" name="photo">
  <button type="submit">Upload</button>
</form>

The key part is enctype="multipart/form-data" — without it the file itself is not sent, only its name. The input's name ("photo") is how PHP finds it.

Reading $_FILES

<?php
  $file = $_FILES["photo"];
  echo $file["name"];      // original name
  echo $file["size"];      // size in bytes
  echo $file["tmp_name"];  // temporary path on server
?>

PHP puts the upload in $_FILES["photo"]. The important field is tmp_name — the temporary location where PHP saved the file until you move it.

Checking type and size

<?php
  $file = $_FILES["photo"];
  $allowed = ["jpg", "jpeg", "png"];
  $ext = strtolower(pathinfo($file["name"], PATHINFO_EXTENSION));

  if ($file["size"] > 2 * 1024 * 1024) {
      echo "File too big (max 2MB)";
  } elseif (!in_array($ext, $allowed)) {
      echo "Only JPG and PNG allowed";
  } elseif (!getimagesize($file["tmp_name"])) {
      echo "Not a real image";
  } else {
      echo "OK to save";
  }
?>
OK to save

We check the size, allow only safe extensions, and confirm it is a genuine image with getimagesize(). All three checks matter — a bad file can be disguised.

Saving the file

<?php
  $newName = "uploads/" . uniqid() . "." . $ext;
  if (move_uploaded_file($file["tmp_name"], $newName)) {
      echo "Uploaded as $newName";
  }
?>
Uploaded as uploads/64f3a1.jpg

We generate a unique name with uniqid() (never trust the user's file name) and move the file with move_uploaded_file, which also confirms it was a real upload.

Security tips

  • Rename the file to a value you generate — do not keep the user's name.
  • Allow only image extensions and verify with getimagesize().
  • Limit the size to avoid huge uploads filling your disk.
  • Never allow .php or other executable files to be uploaded.

Common mistakes

  • Missing enctype. Then the file is not sent, only its name.
  • Trusting the browser's file type. Verify with getimagesize(), not just the reported type.
  • Keeping the user's file name. Rename it to avoid clashes and hidden scripts.
  • Using rename instead of move_uploaded_file. The latter also checks it was a genuine upload.

Summary

  • Uploads need enctype="multipart/form-data" on the form.
  • PHP receives the file in $_FILES, with a tmp_name path.
  • Check size, allowed extension, and confirm with getimagesize().
  • Save with move_uploaded_file under a generated name.
  • Never keep the user's name or allow .php uploads.

Next, validate the surrounding form with form validation, or store the file path in MySQL.

Frequently Asked Questions

How does file upload work in PHP?
The HTML form sends the file with enctype="multipart/form-data". PHP receives it in the $_FILES array, which holds the file's name, type, size and a temporary path. You then move it to a permanent folder.
Why must the form have enctype="multipart/form-data"?
Without it, the browser sends only the file name, not the file itself. The multipart/form-data encoding tells the browser to send the actual file content, so PHP can receive it.
How do I check the uploaded file is really an image?
Check the size, then verify it is an image with getimagesize() (which fails for non-images), and allow only safe extensions like jpg and png. Do not trust the file name or the browser-reported type alone.
What does move_uploaded_file do?
PHP first saves the upload to a temporary location. move_uploaded_file() moves it from there to your chosen folder. Using this function (not a plain rename) also confirms the file was a real upload, which is safer.
How do I make uploads secure?
Validate type and size, rename the file to something you generate (never trust the user's name), store uploads outside the web root or block script execution in that folder, and never allow uploading of .php files.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicShopping Cart Logic Your progress: 42 / 50 (84%) · Lesson 43 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free