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

Courses