How to upload a file in php with php validation of extension?

Simply copy and paste this code on your page
And your input file name will be uploadedfile
Like <input type="file" name="uploadedfile" />

and on submit of this page you will add this code
$youruniqueid could be anything like userid or projected
on the basis of this it will create folder for image upload


$projId=$youruniqueid;
   if(!empty($_FILES['uploadedfile']['name'])){
    $imgExtension = array("jpg","jpe","jpeg","gif","png","GIF","JPG","JPEG","txt","pdf","PDF","docx","xlsx","pptx","pptm","doc","dot","xls","ppt");
   
    $image_name = pathinfo($_FILES['uploadedfile']['name']);
    $extension = strtolower($image_name['extension']);
   
    if(in_array($extension,$imgExtension)){
   
  
     $file_name = $_FILES['uploadedfile']['name'];
     $ext = end(explode('.',$file_name));
  
    
     if ($_SERVER['HTTP_HOST'] == 'localhost')
     $folder_path = 'files/project_attachment/'.$projId; // Relative to the root
    
    else
     $folder_path = '/files/project_attachment/'.$projId; // Relative to the root
    
     $save_path  = $folder_path.'/'.md5(date('ymdhis')).'.'.$ext;
    
// for create a new folder like  /files/project_attachment/554/
if(!is_dir($folder_path)){
     mkdir($folder_path, 0777, TRUE);
     chmod($folder_path , 0777);
     }
    
    
                 if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'] , $save_path))
                {
                     echo "upload successfully";
                }
     }
     else
     {
      echo "Please upload suitable attachment";
     }
   }

Comments

  1. This site is truly amazing my one of friend told me about this. And the theme is quite beautiful as well as the updated stuff also attract visitors to come and visit again. I would definitely bookmark it and would come back to you soon. Keep on updating.

    ReplyDelete

Post a Comment