Image Upload|

  • Normal Upload
  • Using Function
  • Image Type Validation
  • Size Restriction
// Image Upload

<?php

    if(!empty($_FILES["photo"]["name"]))
    {
      $pathphoto=$_FILES["photo"]["name"];
      $tmppath=$_FILES["photo"]["tmp_name"];
      $upload_dir = 'images/'.$empcode.'/';
      mkdir($upload_dir);
      move_uploaded_file($tmppath,$upload_dir.basename('passport'.'.jpg'));
      $pho=$upload_dir.'passport'.'.jpg';
      $sql1 = $conn->prepare("UPDATE `employee` SET `photo`=:photo WHERE `emp_code`=:emp_code");
      $result=$sql1->execute(array(':photo'=>$pho,':emp_code'=>$empcode));
    }

?>

// Image Upload Using Function

<?php

$name = ''; $type = ''; $size = ''; $error = '';
      function compress_image($source_url, $destination_url, $quality) {

      $info = getimagesize($source_url);

          if ($info['mime'] == 'image/jpeg')
          $image = imagecreatefromjpeg($source_url);

          elseif ($info['mime'] == 'image/gif')
          $image = imagecreatefromgif($source_url);

          elseif ($info['mime'] == 'image/png')
          $image = imagecreatefrompng($source_url);

          imagejpeg($image, $destination_url, $quality);
          return $destination_url;
        }

 if(count($_FILES['certificate']["name"])>0)
        {
          if(!empty($_FILES["certificate"]["name"]))
            {
              $upload_dir = 'documents/'.$rand;
              mkdir($upload_dir);
              $url = 'documents/'.$rand.'/passport.jpg';
              $filename = compress_image($_FILES["certificate"]["tmp_name"], $url, 50);

              $sql1 = $conn->prepare("UPDATE `temp_app` SET `passport_photo`=:passport_photo WHERE `application_id`=:application_id");
              $result=$sql1->execute(array(':passport_photo'=>$url,':application_id'=>$rand));

            }
        }


        ?>

// Image Type Validation

<script>
    function ValidateFileUpload() {
    var fuData = document.getElementById('fileChooser');
    var FileUploadPath = fuData.value;
    if (FileUploadPath == '') {
        //  alert("Please upload an png and jpg file format");
        } 
        else {
                var Extension = FileUploadPath.substring(
                FileUploadPath.lastIndexOf('.') + 1).toLowerCase();

              if (Extension == "jpg" || Extension == "jpeg" || Extension == "JPG"|| 
                Extension == "PNG"|| Extension == "png" || Extension == "JPEG") {
        
                    if (fuData.files && fuData.files[0]) 
                    {
                    var reader = new FileReader();
                    reader.onload = function(e) 
                      {
                        $('#blah').attr('src', e.target.result);
                      }
                    reader.readAsDataURL(fuData.files[0]);
                    }
              } 

  else {
          //  alert("Please Upload Only png and jpeg file");    
                 window.location='';
        }
    }
}    
</script>

// Size Restriction

<script>
$('.fileChooser').change(function() {
  if(this.files[0].size>=270000)
  {
    if(this.files[0].size>=1000000) 
    {
     alert("selected File size too Large");
     $("#"+$(this).attr('id')).val('');     
    }        
  }
  else
  {
    alert("selected File size too Low");
   $("#"+$(this).attr('id')).val('');    
  }
  
});
</script>