php pdo image upload

Solutions on MaxInterview for php pdo image upload by the best coders in the world

showing results for - "php pdo image upload"
Pietro
18 Jul 2019
1
2<?php
3
4require_once "connection.php";
5
6if(isset($_REQUEST['btn_insert']))
7{
8 try
9 {
10  $name = $_REQUEST['txt_name']; //textbox name "txt_name"
11   
12  $image_file = $_FILES["txt_file"]["name"];
13  $type  = $_FILES["txt_file"]["type"]; //file name "txt_file" 
14  $size  = $_FILES["txt_file"]["size"];
15  $temp  = $_FILES["txt_file"]["tmp_name"];
16  
17  $path="upload/".$image_file; //set upload folder path
18  
19  if(empty($name)){
20   $errorMsg="Please Enter Name";
21  }
22  else if(empty($image_file)){
23   $errorMsg="Please Select Image";
24  }
25  else if($type=="image/jpg" || $type=='image/jpeg' || $type=='image/png' || $type=='image/gif') //check file extension
26  { 
27   if(!file_exists($path)) //check file not exist in your upload folder path
28   {
29    if($size < 5000000) //check file size 5MB
30    {
31     move_uploaded_file($temp, "upload/" .$image_file); //move upload file temperory directory to your upload folder
32    }
33    else
34    {
35     $errorMsg="Your File To large Please Upload 5MB Size"; //error message file size not large than 5MB
36    }
37   }
38   else
39   { 
40    $errorMsg="File Already Exists...Check Upload Folder"; //error message file not exists your upload folder path
41   }
42  }
43  else
44  {
45   $errorMsg="Upload JPG , JPEG , PNG & GIF File Formate.....CHECK FILE EXTENSION"; //error message file extension
46  }
47  
48  if(!isset($errorMsg))
49  {
50   $insert_stmt=$db->prepare('INSERT INTO tbl_file(name,image) VALUES(:fname,:fimage)'); //sql insert query     
51   $insert_stmt->bindParam(':fname',$name); 
52   $insert_stmt->bindParam(':fimage',$image_file);   //bind all parameter 
53  
54   if($insert_stmt->execute())
55   {
56    $insertMsg="File Upload Successfully........"; //execute query success message
57    header("refresh:3;index.php"); //refresh 3 second and redirect to index.php page
58   }
59  }
60 }
61 catch(PDOException $e)
62 {
63  echo $e->getMessage();
64 }
65}
66
67?>