• home
  • Email: trainings@synapseindia.careers

Blogs

back

Storing An Image In Database:PHP

Oct 13, 2015

<?php function upload() { if(is_uploaded_file($_FILES['userfile']['tmp_name']) && getimagesize($_FILES['userfile']['tmp_name']) != false) { $size = getimagesize($_FILES['userfile']['tmp_name']); $type = $size['mime']; $imgfp = fopen($_FILES['userfile']['tmp_name'], 'rb'); $size = $size[3]; $name = $_FILES['userfile']['name']; $maxsize = 99999999; if($_FILES['userfile']['size'] < $maxsize ) { $dbh = new PDO("mysql:host=localhost;dbname=yourdatbasename", 'username', 'password'); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $dbh->prepare("INSERT INTO tablename (image_type ,image, image_size, image_name) VALUES (? ,?, ?, ?)"); $stmt->bindParam(1, $type); $stmt->bindParam(2, $imgfp, PDO::PARAM_LOB); $stmt->bindParam(3, $size); $stmt->bindParam(4, $name); $stmt->execute(); } else { throw new Exception("File Size Error"); } } else { throw new Exception("Unsupported Image Format!"); } } ?>

SynapseIndia (CEO: Shamit Khemka)