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

Share this:

  • Facebook
  • X

Related

Php

Post navigation

Previous post
Next post

Related Posts

Php How to Create Relations in Laravel Migration

How to Create Relations in Laravel Migration?

August 18, 2022March 28, 2023

Laravel relation provides almost all feature to create the database schema and one of best feature is create relations in laravel migration. As We know relations are used to create the associations between the tables and there is three types of relations in mysql which is one-to-one, one-to-many, and many-to-many….

Share this:

  • Facebook
  • X
Read More
Php How to Call a POST Rest Api in Laravel

How to Call a POST Rest Api in Laravel ?

June 7, 2022June 7, 2022

In this article we will learn to call an post rest api in Laravel. Whenever we want to access the third party data we need to access the data using the APIs, we send a request to another server means outside our application and they respond with preformatted structure. Post…

Share this:

  • Facebook
  • X
Read More

how to add multiple select in laravel eloquent ?

December 29, 2021December 29, 2021

Laravel eloquent builder has rich features to modify the query. In some cases we wanted to modify the select statement on basis of few conditions and wanted to add some select statement on condition. so in this article we will learn to add multiple select in laravel eloquent by preserving…

Share this:

  • Facebook
  • X
Read More

Leave a ReplyCancel reply

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 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • 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

  • How to Call Controller Method from Another Controller in Laravel ?
  • How to Get Domain Name in Laravel ?
  • How to Append Query String to Route in Laravel ?
  • How to Append URL Query Params to Pagination Laravel ?
  • How to Get Today Records in Laravel ?
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version