Validate upload file php

This tutorial explains how to validate upload file php. Here it checks the allowed file extensions and verify file sizes.

Please check the tutorial which explains how to send upload file as email attachment and send all the files in the folder as email attachment.

$name_of_uploaded_file =basename($_FILES['uploaded_file']['name']);
$formData = $_POST;
getFile( $name_of_uploaded_file, $formData );
function getFile( $filename , $formData ) {
    
    $allowedExts = array("csv","pdf");
    $temp = explode(".", $_FILES["uploaded_file"]["name"]);
    $extension = end($temp);
    $mimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv');

    if (in_array($_FILES['uploaded_file']['type'],$mimes )
    && ($_FILES["uploaded_file"]["size"] < 2000000)
    && in_array($extension, $allowedExts))
      {
      if ($_FILES["uploaded_file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["uploaded_file"]["error"] . "<br>";
        }
      else
        {      
            sendMailAsAttachment($_FILES["uploaded_file"]["tmp_name"],$_FILES["uploaded_file"]["name"],$formData);         
        }
      }
    else
      {
      echo "Invalid file";
      }  
}

Share this Tutorial