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 Interface
Mar 22, 2023 programming

PHP Interface

Today we are going to talk about interfaces in PHP. Interface is like a skeleton for the class. Interface is an important topic in OOP and it helps you manage your code better. ...

6 Min Read Read More
PHP catch exceptions
Feb 14, 2023 programming

PHP catch exceptions

Today we are going to talk about catching exceptions in PHP. Most of the time in your script you might face some exceptions. So today we are going to learn how to catch those exceptions. ...

7 Min Read Read More
Deploy PHP to Heroku
Feb 14, 2023 programming

Deploy PHP to Heroku

Today we are going to talk about deploying your PHP on heroku. Having your code on your local machine is one thing and deploying it on a server and sharing it with the world is another thing. ...

7 Min Read Read More
React Beginner to Advance 2025: A Complete Guide to Building Modern Web Applications
Jul 18, 2025 programming

React Beginner to Advance 2025: A Complete Guide to Building Modern Web Applications

React JS has firmly established itself as one of the most widely adopted JavaScript libraries for building dynamic user interfaces. Whether you're crafting a small personal website or architecting a large-scale enterprise dashboard, React's component-based architecture and flexible ecosystem allow developers to create highly performant, scalable, and maintainable applications. ...

29 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 ...