Introduction
Hello friends, Welcome to today's blog about validation, uploading, and fetching images from the database.
Most of the time we are dealing with text data but when you are working on a large e-commerce site that time you need to upload images in coding almost for every important model.
Step 1: Create simple form with input type file
$_FILES array()
array(1) {
["image"]=>
array(6) {
["name"]=>
string(5) "1.png"
["full_path"]=>
string(5) "1.png"
["type"]=>
string(9) "image/png"
["tmp_name"]=>
string(25) "/opt/lampp/temp/phpIXsfM9"
["error"]=>
int(0)
["size"]=>
int(781)
}
}
getimagesize() array();
array(6) {
[0]=>
int(200)
[1]=>
int(200)
[2]=>
int(3)
[3]=>
string(24) "width="200" height="200""
["bits"]=>
int(4)
["mime"]=>
string(9) "image/png"
}
<form method="POST" enctype="multipart/form-data" action="controller.php">
<input type="file" name="image" accept="image/png, image/jpg, image/jpeg" required>
<input type="submit" name="btn">
<form>
Step 2: Store image in folder
sudo chmod -Rf 777 folder_path
move_uploaded_file($_FILES['image']['tmp_name'], "images/" . $_FILES['image']['name']);
Step 3: Validation for the image
if(!empty($_FILES['image']['name']) && isset($_FILES['image']['name'])){
//code
}else{
//code
}
if(
(
$_FILES['image']['type'] == 'image/png' ||
$_FILES['image']['type'] == 'image/jpg' ||
$_FILES['image']['type'] == 'image/jpeg'
)
){
//code
}else{
//code
}
$image = getimagesize($_FILES['image']['tmp_name']);
if($image[0] == $image[1] && $image[0] > 100 && $image[1] > 100 && $image[0] < 1000 && $image[1] < 1000){
// code
}else{
// code
}
if($_FILES['image']['size'] < 2097152){
//code
}else{
//code
}
require_once("db.php");
require_once("image.model.php");
$imageClass = new image();
if(!empty($_FILES['image']['name']) && isset($_FILES['image']['name'])){
if(
(
$_FILES['image']['type'] == 'image/png' ||
$_FILES['image']['type'] == 'image/jpg' ||
$_FILES['image']['type'] == 'image/jpeg'
)
){
$image = getimagesize($_FILES['image']['tmp_name']);
if($image[0] == $image[1] && $image[0] > 100 && $image[1] > 100 && $image[0] < 1000 && $image[1] < 1000){
if($_FILES['image']['size'] < 2097152){
$param['image_name'] = filter_var($_FILES['image']['name'], FILTER_UNSAFE_RAW);
$uploadImage = json_decode($imageClass -> uploadImage($param), true);
if($uploadImage['status'] == 200){
move_uploaded_file($_FILES['image']['tmp_name'], "images/" . $_FILES['image']['name']);
echo "Image upload successfully";
exit;
}else{
echo "please try again";
exit;
}
}else{
echo "please select size less then 2MB.";
exit;
}
}else{
echo "please select image in between 100x100 to 1000x1000.";
exit;
}
}else{
echo "please select png or jpg files";
exit;
}
}else{
echo "file not selected";
exit;
}
Step 4: Store the image name in the database
class Database{
private $host = "localhost";
private $dbname = "images";
private $username = "root";
private $password = "";
public $con;
public function connection(){
$this -> con = null;
try{
$this -> con = new PDO("mysql:host=" . $this -> host . ";dbname=" . $this -> dbname, $this -> username, $this -> password);
}catch(PDOException $e){
echo "connection failed" . $e -> getMessage();
}
return $this -> con;
}
}
Step 5: Create model for queries
public function uploadImage($param){
static $uploadImage;
$db = new Database();
$con = $db -> connection();
$sql = "INSERT INTO `data` (`image_name`) VALUES (:image_name)";
$stmt = $con -> prepare($sql);
$stmt -> bindParam(':image_name', $param['image_name'], PDO::PARAM_STR);
try{
$stmt -> execute();
$uploadImage = array('status' => 200, 'error_msg' => null);
}catch(PDOException $e){
$uploadImage = array('status' => 500, 'error_msg' => $e -> getMessage());
}
return json_encode($uploadImage);
}
public function fetchImages(){
static $fetchImages;
$db = new Database();
$con = $db -> connection();
$flag = 'Y';
$sql = "SELECT `image_name` FROM `data` WHERE `flag` = :flag";
$stmt = $con -> prepare($sql);
$stmt -> bindParam(':flag', $flag, PDO::PARAM_STR);
try{
$stmt -> execute();
$data = $stmt -> fetchAll(PDO::FETCH_ASSOC);
$fetchImages = array('status' => 200, 'data' => $data, 'error_msg' => null);
}catch(PDOException $e){
$fetchImages = array('status' => 500, 'data' => null, 'error_msg' => $e -> getMessage());
}
return json_encode($fetchImages);
}
require_once("db.php");
require_once("image.model.php");
$imageClass = new image();
$fetchImages = json_decode($imageClass -> fetchImages(), true);
foreach($fetchImages['data'] AS $values){
echo " img src='images/" . $values['image_name'] . "' width='200'>" . 'br ';
}
© Rich PHP. All Rights Reserved.