Free cookie consent management tool by TermsFeed Generator PHP upload file | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP upload file

PHP upload file

Last Updated on Feb 15, 2023

Introduction

Do you remember when we talked about working with forms in php?

Today we are going to talk about uploading a file. In the example today we are going to upload an image.

Form

Here is our from

<form action="./upload.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="profile" />
    <input type="submit" value="upload"/>
</form>

FILES

You can handle the file with the help of $_FILES superglobal.

$_FILES['profile']

Let’s see what it’s giving us

print_r($_FILES['profile']);
// Array (
//      [name] => WIN_20210911_06_42_48_Pro.jpg
//      [type] => image/jpeg
//      [tmp_name] => C:\xampp\tmp\phpD384.tmp
//      [error] => 0
//      [size] => 169602
// )

Name is the original name

Type is the file type

Tmp_name is a temporary name that php gives you. When you want to upload the file you need this value

Size is the size of the file

Upload

Let’s handle the file upload now. I have a few conditions:

  1. I don’t want the file to be bigger than 3 mb.
  2. I want to make sure it’s an image
  3. And i want to make sure the extension of the file is png and not jpeg or jpg or something else

Let’s do it now

if(!empty($_FILES['profile'])){
   $errors = [];
   $fileName = $_FILES['profile']['name'];
   $fileTempName = $_FILES['profile']['tmp_name'];
   $fileType = $_FILES['profile']['type'];
   $fileSize =$_FILES['profile']['size'];
 
   $fileNameEnd = explode('.',$_FILES['profile']['name']);
   $fileExtention = $fileNameEnd[sizeof($fileNameEnd) - 1];
   $fileExtention = strtolower($fileExtention);

  if($fileType != 'image/png' || $fileExtention!= 'png' ){
       $errors[] = "The Extention is not accepted";
   }
 
   if($fileSize > 3000000){
       $errors[] = "File Size Must be less than 3 mb";
   }
 
   if(empty($errors)){
       move_uploaded_file($fileTempName,"./images/".$fileName);
       echo "file uploaded.";
   }else{
       echo "file could not be uploaded.";
       echo implode(" ",$errors);
   }
}

There are other ways to make sure the file type is correct and the extension is correct. the way I showed is just one way. Our main focus is on uploading the file.

Move_uploaded_file is a function that helps us move the uploaded file to the directory we want. It gets the tempname as the first argument and the new path as the second argument.

Size is in bytes so for less than 3mb we said approximately less than 3000000 bytes.

Conclusion

Now you know about handling file uploads in PHP.

I recommend you to open a PHP files and create a form and try to upload a file. try to check all the values you get and save the file in a folder.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • upload a file in php
  • working with FILES superglobal variable
  • check the format and extension of the file
  • save the uploaded file on the server

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP Regular Expressions
Mar 22, 2023 programming

PHP Regular Expressions

Today we are going to learn about regular expressions in PHP. A regular expression is a sequence of characters that specifies a search pattern. it's important to know how to apply them in PHP. ...

5 Min Read Read More
Creating a Simple Jupyter Notebook in PHP
Aug 28, 2024 programming

Creating a Simple Jupyter Notebook in PHP

This tutorial will guide you through the steps to create a simple PHP-based notebook interface. The notebook allows you to write and run PHP code in a web browser, maintaining the state between code executions. ...

28 Min Read Read More
AngularJS Beginner to Advance 2025: The Complete Guide for Modern Web Developers
Jul 18, 2025 programming

AngularJS Beginner to Advance 2025: The Complete Guide for Modern Web Developers

In today’s web development landscape, AngularJS may no longer dominate headlines, but it still powers countless legacy applications. Many organizations—especially in finance, healthcare, and enterprise software—rely on AngularJS systems that require ongoing maintenance and enhancements. ...

43 Min Read Read More
Vue.js Beginner to Advanced 2025: Complete Guide
Jul 21, 2025 programming

Vue.js Beginner to Advanced 2025: Complete Guide

Vue.js has evolved into one of the most popular JavaScript frameworks for building dynamic web applications. it continues to strike a balance between simplicity and capability, offering a progressive approach to front-end development. ...

33 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...