Simple PHP code to upload a file (pdf , doc,docx)

Here is the simple php code to upload a pdf file

/* file upload */

 $allowedExts = array("pdf", "doc", "docx"); 
 $targetfolder = $resume_upload_path; // UPLOAD path e.g '/home/public_html/upload'
 $random_num=rand(1000,9999);
$targetfolder = $targetfolder . $random_num.str_replace(' ', '_', basename($_FILES['resume_pdf']['name'])) ;
$targetpath = $root_upload_path . $random_num.str_replace(' ', '_', basename($_FILES['resume_pdf']['name'])) ;

$file_type=$_FILES['resume_pdf']['type'];
$extension = end(explode(".", $_FILES["resume_pdf"]["name"]));


 if ( 12000 < $_FILES["resume_pdf"]["size"]  ) {
 echo "error";  
 exit;
 }

 if ( ! ( in_array($extension, $allowedExts ) ) ) {

  echo "error";  
    exit;
  
 }
 $file_path="";
 if(move_uploaded_file($_FILES['resume_pdf']['tmp_name'], $targetfolder))

 {

 $file_path=$targetpath ;

  }

 else {


  echo "error";  
    exit;
 

 }
 
/* file upload end*/

where $file_path is the path of uploaded file

My Thought

Your email address will not be published. Required fields are marked *

Our Tool : hike percentage calculator