Wednesday, 19 October 2016

Dynamic Image Gallery using PHP

03:15

Dynamic Image Gallery using PHP:

(1)CREATE TABLE images using following details:

create table images
(
Image_Id  int primary key auto_increment,
Image_Title  varchar(200),
 Image_Name   varchar(200)
   
    );

(2)write    code    for    form.html    file:
<html>
<body>
<form enctype="multipart/form-data" action="img_db.php" method="POST">
Image Title: <input type="text" name="img_title" /><br /><br />
Image Description: <input type="text" name="img_desc" /><br /><br />
Choose a file to upload: <input name="uploadedfile" type="file" /><br /><br />
<input name="submit" type="submit" value="submit" />
</form>
</body>
</html>

(3)write code  for  “img_db.php”   file:
<?php
if(isset($_POST["submit"]))
{
$img_title=$_POST["img_title"];
$img_desc=$_POST["img_desc"];
}
$img_name=$_FILES["uploadedfile"]["name"];
if (($_FILES["uploadedfile"]["type"]=="image/gif"
|| $_FILES["uploadedfile"]["type"]=="image/jpeg"
|| $_FILES["uploadedfile"]["type"]=="image/pjpeg"
&& $_FILES["uploadedfile"]["size"]<20000))
{
if ($_FILES["uploadedfile"]["error"]>0)
{
echo "Return Code:".$_FILES["uploadedfile"]["error"]."<br />";
}
else
{
$i=1;
$success=false;
$new_img_name=$img_name;
while(!$success)
{
if (file_exists("uploads/".$new_img_name))
{
$i++;
$new_img_name="$i".$img_name;
}
else
{
$success=true;
}
}
move_uploaded_file($_FILES["uploadedfile"]["tmp_name"],"uploads/".$new_img_name);
echo "Stored in: "."uploads/".$new_img_name;
echo "<br />";
$con=mysql_connect("localhost","root","");
mysql_select_db("test",$con);
$qry="INSERT INTO images(Image_Title,Image_Name,img_filename)
VALUES('$img_title','$img_desc','$new_img_name')";
if(!mysql_query($qry))
{
die("An error".mysql_error());
}
else
{
echo "1 row added";
}
}
}
else
{
echo "Invalid file";
}
?>

(4)write code for  display.php file  which will display your gallery:
<?php

$con=mysql_connect("localhost","root","");
mysql_select_db("test",$con);
$qry="SELECT * FROM images";
$result=mysql_query($qry);
//$row=mysql_fetch_array($result);

while($row=mysql_fetch_array($result))
{
echo "
<img
src=uploads/".$row["img_filename"]."  height=\"400\"
width=\"400\"/>";
}

?>

output:



Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

1 comments:

  1. Thank you so much for posting this programming of dynamic image gallery. i was looking PHP training and i found this. really helpful.

    ReplyDelete

PHP Training Mumbai, PHP MySQL Training Mumbai, PHP Course Mumbai, PHP Institute Mumbai

 

© 2013 PHP TRAINING IN MUMBAI BY OM SIR . All rights resevered. Designed by OM SIR

Back To Top