Image upload in php pdo with database | php upload image to folder | php code for image upload and display


Image upload in php pdo with database | php upload image to folder | php code for image upload and display

Step :1
Create Folder "uploads" where your all project files located 

Step :2

Create Db Of Name "imageupload"

After that you need to create table:


CREATE TABLE `users` (
`id` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`image` VARCHAR( 25 ) NOT NULL
)


Step : 3 

Create Connection File Name Write As "Connect.php"




<?php

try{
$con = new PDO ("mysql:host=localhost;dbname=imageupload","root",""); 
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "connected";
}
catch(PDOException $e)
{
echo "error:".$e->getMessage(); 
}

?>

Step : 4 

Create Index.Php File 



<?php 
include "connect.php";
if(isset($_POST['ok'])) 

$folder ="uploads/"; 
$image = $_FILES['image']['name']; 
$path = $folder . $image ; 
$target_file=$folder.basename($_FILES["image"]["name"]);
$imageFileType=pathinfo($target_file,PATHINFO_EXTENSION);
$allowed=array('jpeg','png' ,'jpg'); $filename=$_FILES['image']['name']; 
$ext=pathinfo($filename, PATHINFO_EXTENSION); if(!in_array($ext,$allowed) ) 

 echo "Sorry, only JPG, JPEG, PNG & GIF  files are allowed.";
}
else{ 
move_uploaded_file( $_FILES['image'] ['tmp_name'], $path); 
$sth=$con->prepare("insert into users(image)values(:image) "); 
$sth->bindParam(':image',$image); 
$sth->execute(); 


?> 
<form method="POST" enctype="multipart/form-data"> 
<input type="file" name="image" /> 
<input type="submit" name="ok"/> 
</form>
<a href="select.php">See Image</a>

Step : 5 

Create Select.Php File



<table border="2">
<tr>
<th>ID</th>
<th>Image</th>
</tr>
<?php
include "connect.php";
$select = $con->prepare("SELECT * FROM users ");
$select->setFetchMode(PDO::FETCH_ASSOC);
$select->execute();
while($data=$select->fetch()){
?><tr>
<td><?php echo $data['id']; ?></td>
<td><img src="uploads/<?php echo $data['image']; ?>" width="100" height="100"></td>
<?php
}?>
</tr></table>
<a href="index.php">Add new image</a>

8 comments:

  1. I was able to uplaod image for the first time. But for the second it was not uploading

    ReplyDelete
  2. it's working.but when I was check the upload folder. this is empty and not fetch data

    ReplyDelete
  3. Thank you for sharing a bunch of this quality contents, I have bookmarked your blog. Please also explore advice from my site. I will be back for more quality contents. erklärvideos

    ReplyDelete
  4. You need some press and backing from online film bloggers to make your movie standout in the eyes of movie distributors.Fmovies

    ReplyDelete
  5. bruka od tutoriala ne dela pou kurca

    ReplyDelete

Pages