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 install laravel composer

How to install laravel 8 ?

September 20, 2021May 6, 2022

Laravel is most popular framework of PHP. It provides lots of in-build tools and library to build the web application. Laravel is used to to create robust custom web applications with ease of amazing developer experience. Laravel is easily customizable and provides tools like ORM, Validation libraries, Queue, Mail, Routing,…

Read More
Php How to Add or Update Index in Laravel Migration

How to Add or Update Index in Laravel Migration?

August 21, 2022March 16, 2024

Add or Update Index in Laravel Migration can be implement easily using the migrations libraries index method. Migrations provides almost all feature to create the database schema and create and update index in laravel migration. As We know database index is used to improve the speed of tables. Index can…

Read More

Create custom library or custom class in Laravel 8 / 9

December 21, 2021November 6, 2023

Laravel provides various packages and library to enhance our application features and functionalities but sometime we want our custom class in laravel to handle the few features of the application. Thus I this article i will show you to create a class and use it using the alias or directly…

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

  • 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

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version