php pdo upload image code example
Example: php pdo image upload
<?php
require_once "connection.php";
if(isset($_REQUEST['btn_insert']))
{
try
{
$name = $_REQUEST['txt_name'];
$image_file = $_FILES["txt_file"]["name"];
$type = $_FILES["txt_file"]["type"];
$size = $_FILES["txt_file"]["size"];
$temp = $_FILES["txt_file"]["tmp_name"];
$path="upload/".$image_file;
if(empty($name)){
$errorMsg="Please Enter Name";
}
else if(empty($image_file)){
$errorMsg="Please Select Image";
}
else if($type=="image/jpg" || $type=='image/jpeg' || $type=='image/png' || $type=='image/gif')
{
if(!file_exists($path))
{
if($size < 5000000)
{
move_uploaded_file($temp, "upload/" .$image_file);
}
else
{
$errorMsg="Your File To large Please Upload 5MB Size";
}
}
else
{
$errorMsg="File Already Exists...Check Upload Folder";
}
}
else
{
$errorMsg="Upload JPG , JPEG , PNG & GIF File Formate.....CHECK FILE EXTENSION";
}
if(!isset($errorMsg))
{
$insert_stmt=$db->prepare('INSERT INTO tbl_file(name,image) VALUES(:fname,:fimage)');
$insert_stmt->bindParam(':fname',$name);
$insert_stmt->bindParam(':fimage',$image_file);
if($insert_stmt->execute())
{
$insertMsg="File Upload Successfully........";
header("refresh:3;index.php");
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
?>