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 Variables
Mar 21, 2023 programming

PHP Variables

Today we are going to learn about variables in PHP. Variables are very important and powerful in any programming language. So if you are learning PHP it's very important to know about variables. ...

5 Min Read Read More
PHP Date and Time
Mar 21, 2023 programming

PHP Date and Time

Today we are going to learn about date and time in PHP. knowing how to work with dates and times and how to work with different formats of dates is very important. ...

14 Min Read Read More
PHP Validate Email
Feb 15, 2023 programming

PHP Validate Email

Today we are going to talk about validating emails in PHP. In a lot of projects you need to validate the emails given by the user to make sure the email address has a correct format. ...

4 Min Read Read More
PHP Simple Web Crawler
Mar 22, 2023 programming

PHP Simple Web Crawler

Today we are going to build a simple web crawler in PHP and parse the box office movies from IMDB. We can do it with PHP easily. We don’t even need any extra package. ...

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