Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
upload file in php

How to upload file in php ?

Aman Jain, September 13, 2021September 29, 2021

In this tutorial, we will learn basic of uploading of a file in php and also basic configuration required for php file upload.

Configure the php.ini for large file upload

By default php support 2 mb of file upload but in real world we require more than 2 mb. Same as file size php execution time is limited to 30 seconds.

So, Firstly we will change these two settings by which we will be able to upload files greater than 2 mb.

Read this article for how to change and find the php.ini and variables:

; Whether to allow HTTP file uploads.
file_uploads = On
 
; Maximum allowed size for uploaded files.
upload_max_filesize = 20M
 
; Maximum size of POST data that PHP will accept.
post_max_size = 20M
 
max_execution_time = 300

Here, we are changing the execution time of php to 300 seconds, file size to 20 MB and post request size to 20 mb as well.

Now, let’s create the real example of file uploading by following below simple steps:

Step 1 . Create a html file

First step is create html form which support file uploading. To upload a file in a form it’s much important to add the enctype=”multipart/form-data” otherwise form uploading will not work.

Here is the file index.php

<!DOCTYPE html>
<html>
<head>
  <title>Readerstacks- PHP File Upload</title>
</head>
<body>
 
  <form method="POST" action="upload.php" enctype="multipart/form-data">
    <div class=“row”>
 
       <input type="file" id="upload" name=“image”> 
    </div>
 
    <input type="submit"  name=“submit_btn”  value="Upload" />
  </form>
</body>
</html>

In the above html form tag and form tag contains method=“POST” , action=”upload.php”  and enctype=”multipart/form-data”.

We can only upload in method=“POST” and enctype=”multipart/form-data” otherwise, it will not work.

action=“upload.php” is the file name of php script.

Step 2 : Create a php file to handle the form upload

Now, we are going to create a file upload.php and handle the logic of form submit.

<?php 

$message="";
if (isset($_POST['submit_btn']) && $_POST['submit_btn'] == "Upload") {

    if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {

        $fileTmpPath    = $_FILES['image']['tmp_name'];
        $file_name      = $_FILES['image']['name'];
        $file_size      = $_FILES['image']['size']; // if you want to check the file size(ex: 20 mb)
        
        $file_type      = $_FILES['image']['type'];// if you want to check the file type(jpg, png,zip)
        
        $uploadFileDir = './uploaded_files/';
        $newFileName= rand(9999,99999999).time()."_".$file_name ;
        $dest_path = $uploadFileDir . $newFileName;
        
        
        if(move_uploaded_file($fileTmpPath, $dest_path))
        {
            $message="File is successfully uploaded.";
        }
        else
        {
            $message = "There was some error moving the file to upload directory. Please make sure the upload directory is writable by web server.";
        }
    }
    else{
        $message = "There was some error moving the file to upload directory. Please make sure the upload directory is writable by web server.";
    }
    echo $message ;
}
else{
    echo "Not a form";
}

Here, In first line we are checking $_POST[‘submit_btn’] is set and value is Upload, then we fetch the values from $_FILES variable.

Then, we are creating a dynamic unique name for file so it won’t conflict with other.

$newFileName= rand(9999,99999999).time()."_".$file_name ;

After it , we are using move_uploaded_file function to move the file from temporary location to uploaded_files folder location.

Make sure uploaded_files folder is in same directory where php file is exist and permission are sufficient to upload the file. Permission should be:

Php user and group of folder

$ sudo chown -R www-data:www-data ./uploaded_files

Php folder permission

$ sudo chmod -R 755 ./uploaded_files

Screenshot:

  • Screenshot 2021 09 13 at 11.30.28 PM
  • Screenshot 2021 09 13 at 11.30.36 PM

Related

Php

Post navigation

Previous post
Next post

Related Posts

Php How to Update Enum Field Value using Laravel Migration

How to Update Enum Field Value using Laravel Migration ?

August 13, 2022March 16, 2024

Sometime after add the values in enum field we wanted to Update Enum Field Value using Laravel Migration so that we can work according to requirements. In our last article How to Create Enum Field in Laravel Migration ? we learnt to create the enum and today we are give…

Read More
Php Laravel Custom Facade with example

How to create a custom facade with example in Laravel 8 ?

December 22, 2021January 10, 2022

In Laravel there several facades to use like DB, URL, Validator ,Request etc. Facades are used to use the class methods statically using the application service container. In this article we will learn to create our own facades with service container. Let’s start with step by step For a better…

Read More
Php How to Implement JWT Auth in Laravel

How to Implement JWT Auth in Laravel 9 ?

May 31, 2022August 20, 2022

JWT (Json Web Token) is used to create the authentication between two parties client and server that means it creates a secure information transactions between two parties. In the process of php-open-source-saver/jwt-auth token it creates a token claims and generates a token behalf of these claims then it verify on…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version