DEV Community

popoola Temitope
popoola Temitope

Posted on

2 2

Uploading file using html and php

uploading file in php

  • HTML Part
  • create a html form that contain the file upload input, Like the one one below.

    
    <!DOCTYPE html>
    <html>
    <body>
    
    <form action="upload.php" method="post" enctype="multipart/form-data">
      <label>Select image to upload:<label><br>
      <input type="file" name="fileName" id="">
      <input type="submit" value="Upload File" name="upload">
    </form>
    
    </body>
    </html>
    
    
    
    
    Enter fullscreen mode Exit fullscreen mode

    note: add enctype="multipart/form-data" to the form attribute, to specify the content-type



  • PHP Part

  • Create upload.php file and add the PHP code below to it.
    
    <?php
    
    if(isset($_POST["upload"])) {
    $image_dir = "image/"; //folder to store the file
    $image_file = $image_dir . basename($_FILES["fileName"]["name"]); //directory of the file with the file name
      if (($_FILES["fileName"]["size"] < 500000)&&($_FILES["fileName"]["size"] >0)) { //check if file is between 1kb and 50GB size
        if (move_uploaded_file($_FILES["fileName"]["tmp_name"], $image _file)) { // store the file on the server
          echo "file upload successful";
        } else {
          echo "there was an error uploading your file.";
        }
      } else {
        echo "file size is too small/big";
    
      }
    }
    ?>
    
    
    
    Enter fullscreen mode Exit fullscreen mode

    feel free to leave comment

    Sentry image

    See why 4M developers consider Sentry, “not bad.”

    Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

    Learn more

    Top comments (0)