Created
October 18, 2018 10:58
-
-
Save mahamudul310/9ce8ac7ba9b945e53953803cb7fe6885 to your computer and use it in GitHub Desktop.
How to upload image in oop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=============================FONTEND FORM=============================== | |
<form class="form-horizontal" action="" method="post" enctype="multipart/form-data"> | |
<input type="file" name="image" accept="image/*" multiple /> | |
===========================BACKEND================================ | |
if(isset($_POST['save'])){ | |
$message = $blog->save_image_info($_POST); | |
} | |
$query_result = $blog->selectAllimageinfo(); | |
============================FUNCTION====================================== | |
public function save_image_info() { | |
$image_name = $_FILES['image']['name']; | |
$directory ='../asset/blog_image/'; | |
$image_url =$directory.$image_name; | |
$image_type = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION); | |
$image_size = $_FILES['image']['size']; | |
$check = getimagesize($_FILES['image']['tmp_name']); | |
if($check){ | |
if(file_exists($image_url)){ | |
die('This file already exist. Please try anotyer one'); | |
} else { | |
if($image_size>5000000){ | |
die('File Size too large.'); | |
} else { | |
if($image_type != 'jpg' && $image_type != 'png'){ | |
die('File is not valid. Please use JPG or PNG'); | |
} else { | |
move_uploaded_file($_FILES['image']['tmp_name'],$image_url); | |
$sql ="INSERT INTO tbl_image (image) VALUES('$image_url')"; | |
mysqli_query($this->connection, $sql); | |
echo 'Image upload successfully'; | |
} | |
} | |
} | |
} else { | |
die('The file you upload is not an image. Please upload a valid image file !'); | |
} | |
} | |
public function selectAllimageinfo(){ | |
$sql="SELECT * FROM tbl_image"; | |
if(mysqli_query($this->connection, $sql)){ | |
$query_result = mysqli_query($this->connection, $sql); | |
return $query_result; | |
} else { | |
die('Query Problem'. mysqli_error($this->connection)); | |
} | |
} | |
============================FRONT END VIEW================================== | |
<div class="container"> | |
<div class="row"> | |
<?php while ($image_info = mysqli_fetch_assoc($query_result)) { ?> | |
<div class="col-md-3"> | |
<div class="well"> | |
<img src="<?php echo $image_info['image']; ?>" class="img-responsive" /> | |
</div> | |
</div> | |
<?php } ?> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment