php file uploading

File Uploading is a common feature in today’s PHP applications. You will first have to understand the basics concept of file uploading in PHP.
A PHP script can be used with a HTML form to allow users to upload files to the server. Initially files are uploaded into a temporary directory and then relocated to a target destination by a PHP script.

Configure The “php.ini” File

First, ensure that PHP is configured to allow file uploads or not.
In your “php.ini” file, search for the file_uploads directive and check file_uploads set On or not:

file_uploads = On

Just follow below steps for uploading a file.

  • Than clicks on the browse button and selects a file to upload from your local PC.
  • After select file full path to the selected file appears in the text filed then the user clicks the submit button.
  • The selected file is sent to the temporary directory on the server.
  • The PHP script that was specified as the form handler in the form’s action attribute checks that the file has arrived and then copies the file into an intended directory.
  • Finally PHP script show the success message.

Create The HTML Form

Create an HTML form that allow users to choose the file they want to upload:

Note: for upload any file form enctype attribute value should be enctype=”multipart/form-data” and make sure the form method should be post method=”post”.

<!DOCTYPE html>
<html>
<body>

<form action="uploadfile.php" method="post" enctype="multipart/form-data">
    Select file to upload:
    <input type="file" name="fileUpload" id="fileUpload">
    <input type="submit" value="Click Me Upload File" name="submit">
</form>

</body>
</html>

Create The Upload File PHP Script

The “uploadfile.php” file contains the code for uploading a file:

There we are using a global PHP variable called $_FILES. This variable is an associate double dimension array and keeps all the information related to uploaded file. So if the value assigned to the input’s name attribute in uploading form was file, then PHP would create following five variables −

  • $_FILES[‘file’][‘tmp_name’] − the uploaded file in the temporary directory on the web server.
  • $_FILES[‘file’][‘name’] − the actual name of the uploaded file.
  • $_FILES[‘file’][‘size’] − the size in bytes of the uploaded file.
  • $_FILES[‘file’][‘type’] − the MIME type of the uploaded file.
  • $_FILES[‘file’][‘error’] − the error code associated with this file upload.
<?php

if(isset($_FILES['fileUpload']))
{
      $file_name = $_FILES>['fileUpload']['name'];
      $file_size = $_FILES['fileUpload']['size'];
      $file_tmp = $_FILES['fileUpload']['tmp_name'];
      $file_type = $_FILES['fileUpload']['type'];
      $file_ext = strtolower(end(explode('.',$_FILES['fileUpload']['name'])));
      move_uploaded_file($file_tmp, "uploads/".$file_name);
      echo "File has been uploaded!";
}
?>

How check if file already exists or not

Here we are checking if the uploading file already exists in the “uploads” folder. If it is, than display the error message otherwise display success message.

<?php
if(isset($_FILES['fileUpload']))
{
    $file_name = $_FILES>['fileUpload']['name'];
    $file_tmp = $_FILES['fileUpload']['tmp_name'];
    
    // Check if file already exists
    if (file_exists($target_file))
    {
       echo "Sorry, file already exists!";
    }
    else
    {
        move_uploaded_file($file_tmp, "uploads/".$file_name);
        echo "Yes, File has been uploaded!";
    }
}
?>

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Free online tool to download Instagram videos, photos and save Instagram videos, photos to your device. It's free & easy to use.

Download Now

CURRENCY CONVERTER

Our real time currency converter calculator will convert your money based on current values from around the world. It's free & easy to use.

Convert Now

ROBOST.TXT GEN.

Create robots txt file that help search engines to crawling and indexing your site with help of our free online Robots.txt Generator tool.

Create Now

HTML MINIFIER

Free online tool to minify or compress html code and reduce the size of html code in few clicks. It's free & easy to use.

Minify Now

CSS MINIFIER

Free online tool to minify or compress css code and reduce the size of css code in few clicks. It's free & easy to use.

Minify Now

JAVASCRIPT MINIFIER

Free online tool to minify or compress javascript code and reduce the size of javascript code in few clicks. It's free & easy to use.

Minify Now

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.