Open In App

Write a code to upload a file in PHP ?

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:




<!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>




<!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 


Article Tags :