Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

Creating a photo gallery using PHP

Creating a photo gallery using PHP

In thisĀ  tutorial I am going to show, how to create a photo gallery using PHP. I will not recommend to create gallery in PHP , for creating better photo gallery use java script, Ajax and php.

PHP BASED PHOTO GALLERY:-
The program of photo gallery willĀ  complete in three step. First step Ā is upload.php.Ā  thisĀ  program explain that how image will store in a directory and how files will read from a directory andĀ  how these display in group of three with hyperlink .
[note:- create a directory ā€˜imageā€™ in your project directry]

upload.php

<?php
if(isset($_POST[‘upload’]))
{
$path=”image/”;
$path=$path.$_FILES[‘f_nm’][‘name’];
if($_FILES[‘f_nm’][‘size’]>0)
{
move_uploaded_file($_FILES[‘f_nm’][‘tmp_name’],$path);
echo”The Image “. basename($_FILES[‘f_nm’][‘name’]).” uploaded”;
}
}
/////////READING IMAGE’S NAME FROM IMAGE DIRECTORY/////////
$dp=opendir(“image”);
while($fn=readdir($dp))
{
if($fn!=’.’&&$fn!=’..’&&$fn!=’Thumbs.db’)
$fnm[]=$fn;
}

?>

<form method=”post” action=”” enctype=”multipart/form-data”>
<table width=”372″ border=”1″ align=”center”>
<tr>
<td width=”362″ height=”121″><center>
<?php
$lk=$_REQUEST[‘lid’];
if(empty($lk))
$lk=1;
$ed=$lk*3;
$st=$ed-3;
$c=1;
echo”<table>”;
foreach($fnm as $v)
{
if($c>$st&&$c<=$ed)
{
echo “<td><img src=’image/$v’ height=’50’ width=’50’></td>”;
}
$c++;
}
echo”</table>”;
?></center></td>
</tr>
<tr>
<td><center>
<?php
$count=count($fnm);
$lnk=ceil($count/3);
for($i=1;$i<=$lnk;$i++)
echo “<a href=’upload.php?lid=$i’>$i</a>”.” “;
?></center>
</td>
</tr>
<tr>
<td height=”40″><table width=”362″ border=”1″>
<tr>
<td width=”108″>Upload File </td>
<td width=”218″><input type=”file” name=”f_nm”></td>
</tr>
<tr>
<td><input name=”upload” type=”submit” id=”upload”
value=”Upload”></td>
<td>&nbsp;</td>
</tr>
</table></td>
</tr>
</table>
</form>
Images of directory named ā€˜imageā€™ are displayed in tabular format. Each row of the table has five column .If number of images increase the group of five, they automatic adjust in next rows. Ā Rows and columns appear dynamically according to files in image directory.

gallery.php

<?php
if(isset($_POST[‘upload’]))
{
$path=”image/”;
$path=$path.$_FILES[‘f_nm’][‘name’];
if($_FILES[‘f_nm’][‘size’]>0)
{
move_uploaded_file($_FILES[‘f_nm’][‘tmp_name’],$path);
echo”The Image “. basename($_FILES[‘f_nm’][‘name’]).” uploaded”;
}
}
/////////READING IMAGE’S NAME FROM IMAGE DIRECTORY/////////

$dp=opendir(“image”);
while($fn=readdir($dp))
{
if($fn!=’.’&&$fn!=’..’&&$fn!=’Thumbs.db’)
$fnm[]=$fn;
}

?>

<form action=”” method=”post” enctype=”multipart/form-data”>
<table width=”33%” border=”0″ align=”center” bgcolor=”#99CCCC”>
<tr>
<td height=”258″><center>
<?php
$tot=count($fnm);
$no_row=ceil($tot/5);
$i=1;
echo”<table>”;
for($r=1;$r<=$no_row;$r++)
{
echo “<tr>”;
for($c=1;$c<=5&&$i<=$tot;$i++,$c++)
{
list($k,$v)=each($fnm);
echo “<td><img src=’image/$v’ height=’50’ width=’50’></td>”;
}
echo “</tr>”;
}
echo”</table>”;
?></center>
</td>
</tr>
<tr>
<td height=”68″><table width=”312″ border=”0″>
<tr>
<td width=”81″>Upload File </td>
<td width=”221″><input type=”file” name=”f_nm” /></td>
</tr>
<tr>
<td><input name=”upload” type=”submit” id=”upload”
value=”Upload” /></td>
<td>&nbsp;</td>
</tr>
</table></td>
</tr>
</table>
</form>
The final touch of photo gallery. The concept of master page, and dynamic table creation are implements here.

photogallery.php

<?php
if(isset($_POST[‘upload’]))
{
$path=”image/”;
$path=$path.$_FILES[‘f_nm’][‘name’];
if($_FILES[‘f_nm’][‘size’]>0)
{
move_uploaded_file($_FILES[‘f_nm’][‘tmp_name’],$path);
echo”The Image “. basename($_FILES[‘f_nm’][‘name’]).” uploaded”;
}
}
/////////READING IMAGE’S NAME FROM IMAGE DIRECTORY/////////

$dp=opendir(“image”);
while($fn=readdir($dp))
{
if($fn!=’.’&&$fn!=’..’&&$fn!=’Thumbs.db’)
$fnm[]=$fn;
}

?>

<form action=”” method=”post” enctype=”multipart/form-data”>
<table width=”640″ border=”1″ align=”center”>
<tr>
<td width=”318″ height=”155″><table width=”33%” border=”0″ align=”center”

bgcolor=”#99CCCC”>
<tr>
<td height=”258″><center>
<?php
$tot=count($fnm);
$no_row=ceil($tot/5);
$i=1;
echo”<table>”;
for($r=1;$r<=$no_row;$r++)
{
echo “<tr>”;
for($c=1;$c<=5&&$i<=$tot;$i++,$c++)
{
list($k,$v)=each($fnm);
echo “<td><a href=’photogallery.php?id=$v’><img src=’image/$v’

height=’50’ width=’50’></a></td>”;
}
echo “</tr>”;
}
echo”</table>”;
?>
</center></td>
</tr>
<tr>
<td height=”68″><table width=”312″ border=”0″>
<tr>
<td width=”81″>Upload File </td>
<td width=”221″><input type=”file” name=”f_nm” /></td>
</tr>
<tr>
<td><input name=”upload” type=”submit” id=”upload”
value=”Upload” /></td>
<td>&nbsp;</td>
</tr>
</table></td>
</tr>
</table></td>
<td width=”306″ bgcolor=”#669999″>
<?php
$img=$_REQUEST[‘id’];
if(isset($img))
echo”<img src=’image/$img’ height=’300′ width=’300′>”;
?></td>
</tr>
</table>
</form>

5 1 vote
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Openplus

Very informative tutorial on PHP based photo gallery for beginners

Deon

No matter if some one searches for his essential thing, thus he/she wishes
to be available that in detail, thus that thing is maintained over here.