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 Namespace
Mar 23, 2023 programming

PHP Namespace

Today we are going to talk about namespaces in PHP. Namespace is like putting the classes in a group. it helps you organize your code much better. It's a must-know topic for a PHP programmer. ...

7 Min Read Read More
PHP Abstract Class
Mar 22, 2023 programming

PHP Abstract Class

Today we are going to talk about abstract classes in PHP. Abstract class is another important topic in object oriented programming and today we are going to learn everything about them. ...

8 Min Read Read More
PHP array filter
Feb 14, 2023 programming

PHP array filter

Today we are going to talk about array filter in PHP. array filter is a very useful function and it helps you filter an array by key, value or both. ...

6 Min Read Read More
PHP Password Hashing
Mar 22, 2023 programming

PHP Password Hashing

Today we are going to talk about hashing passwords in PHP. Storing and managing passwords is very important. And in PHP it’s very easy. ...

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