Open In App

Write a code to upload a file in PHP ?

Last Updated : 22 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will understand how to upload a file using PHP. Let us understand some basic configurations.

In your “php.ini” file, search for file_uploads and set it to “On”.

file_uploads = On

Approach:

  • First, we create an HTML file in which we make a form so that we can upload the file using the POST method.
  • The $target_dir variable in “fileupload.php” takes a directory name through a post method.
  • $target_file takes the directory location where the file will be uploaded.
  • $imageFileType is used to take the file extension so that we make sure that the file has a valid extension.
  • $extensions are used to store the valid extensions so you can change them according to the requirements of the program and add or remove them from the array of extensions.
  • In the final step, enter the directory name in which you want to upload the file and then choose the file from the local device and submit it. You will see the file is uploaded as mentioned in the below image.

index.html




<!DOCTYPE html>
<html>
  
<body>
  
    <form action="fileupload.php" method="post" 
          enctype="multipart/form-data">
        Directory<input type="text" name="dirname" 
                        id="dirname"><br>
        Select image to upload:
        <input type="file" name="fileToUpload" 
               id="fileToUpload"><br>
        <input type="submit" value="Upload Image" 
               name="submit">
    </form>
  
</body>
  
</html>


fileupload.php




<!DOCTYPE html>
<?php
  
    $target_dir = $_POST["dirname"]."/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
    $extensions = array("jpeg","jpg","png","pdf","gif");
  
   if(isset($_POST["submit"])) {
  
        // To check whether directory exist or not
        if(!empty($_POST["dirname"])){
            if(!is_dir($_POST["dirname"])) {
                mkdir($_POST["dirname"]);    
                $uploadOk = 1;            
            }
        }
        else {
            echo "Specify the directory name...";
            $uploadOk = 0;
            exit;
        }
  
        // To check extensions are correct or not
        if(in_array($imageFileType, $extensions) === true) {     
            $uploadOk = 1;
        
        else {
  
            echo "No file selected or Invalid file extension...";
            $uploadOk = 0;
            exit;        
        }
    }
        // Check if file already exists
        if (file_exists($target_file)) {
  
            echo "Sorry, file already exists.";
            $uploadOk = 0;
            exit;
        }
  
        // Check file size
        if ($_FILES["fileToUpload"]["size"] > 10000000) {
  
            echo "Sorry, your file is too large.";
            $uploadOk = 0;
            exit;
        }
  
        // Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) 
        {
           echo "Sorry, your file was not uploaded.";
        
         else 
        {
  
            // If everything is ok, try to upload file
            if (move_uploaded_file($_FILES["fileToUpload"]
                  ["tmp_name"], $target_file)) 
            {      
                echo "The file "$_FILES["fileToUpload"]
                  ["name"]. " has been uploaded.";
            
            else 
            {        
                echo "Sorry, there was an error uploading your file.";
            }
        }    
?>
  
</body>
</html>


Output:

write the directory name and select the file

after uploading the file 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments