Image Gallery

Written by @m_k_amin 30 December 2012

This script is an image gallery which make it easy to add and show images just in one page script. It uses both file uploading and MySQL database and configure what it needs automatically. Enjoy!

Code Snippet:

                                                
                                                <!-- this script is provided by https://www.phpfreecode.com coded by: Kerixa Inc. -->
<!-- This Script is from www.hawkee.com, found at www.phpfreecode.com-->
<?php
	$host="localhost"; // Host name
	$username="root"; // Mysql username
	$password=""; // Mysql password
	
function setup(){
echo('
<p style="color: #008000;	text-align: left;	font-size: 15pt;"">-Automatic setup is started...</p>
');
global $host,$username,$password,$link;
//$link=mysql_connect($host, $username, $password);
$sql= 'CREATE DATABASE Pics';
if (!mysql_query ($sql, $link)) die('
<p style="text-align: center;	font-size: 20pt;"><span style="color: #FF0000;">Failed to 
create database! </span><br><span style="font-size: 12pt;">&gt;&gt;Please check the parameters and database server&lt;&lt;</span></p>
');
$sql = "CREATE TABLE `Pics`.`pic` (
`ID` INT NOT NULL ,
`Title` TEXT NOT NULL,
`Pic` TEXT NOT NULL
) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;";

mysql_query($sql) or die('Setup Failed');
echo('
<p style="color: #008000;	text-align: left;	font-size: 15pt;"">-Automatic setup completed successfully. Your Database is ready!</p>
');
}

$link=mysql_connect($host, $username, $password) or die(mysql_error());
$db_name="Pics"; 
$result=mysql_select_db($db_name);
if(!$result){
	setup();
}
$sql="SELECT * FROM pic";
$result=mysql_query($sql);
if ($result) $cntr=mysql_num_rows($result)+1; else $cntr=0;
if ($cntr>1) $r=rand(1,$cntr-1); else $r=0;
if (isset($_POST['Add'])){
	$id=$cntr;
	$title=$_POST['title'];
	if ($_FILES['picf']['error']==0) {
          $tmpFile= $_FILES['picf']['tmp_name'];
          $newFile=getcwd().'/'.$_FILES['picf']['name'];
          if (move_uploaded_file($tmpFile, $newFile)){
          	$path=$_SERVER['HTTP_HOST'].'/'.dirname ($_SERVER['REQUEST_URI']).'/'.$_FILES['picf']['name'];
          	echo '
            <p style="text-align: center;font-size: 17pt;color: #008000;"><strong>Your Picture is Added Successfully.</strong></p>					';
           	$sql="INSERT INTO pic(ID,Title,Pic)VALUES('$id', '$title', '$path')";
			$result=mysql_query($sql);
			if (!$result) die (mysql_error());
			$r=$id;
          }else
          	echo '
            <p style="text-align: center;font-size: 17pt;color: #FF0000;"><strong>An Error Occurred While Uploading.</strong></p>
			';
	}else
          echo 'An Error occuerd NO.: '.$_FILES['userfile']['error'].'<br>';
}

if (isset($_GET['no'])){
	$r=$_GET['no'];
	$sql="SELECT * FROM pic WHERE ID='$r'";
	$result=mysql_query($sql);
	$home=$_SERVER['PHP_SELF'];
	$rpic=mysql_fetch_array($result);
	$d=$rpic['Pic'];
	die( "
	<body style='background-color: #000000'>
	<div style='text-align: center'>
		<img alt='No Image' height='768' src='http://$d' style='vertical-align: middle; padding: 0px; margin: 0px; border: thick outset #FFFFCC' width='1024'>
	</div>
	<p style='text-align: center; color: #F8DFDF; font-family: Times New Roman, Times, serif; font-size: 30pt'>
	<a href='$home'>Back</a></p>
	</body>
	");
}
?>


<table style="border: 1px solid #000000;width: 456px" align="center">
	<tr>
		<td style="font-family: 'Times New Roman', Times, serif;font-size: 17pt;text-align: center;width: 734px; color: #D9EEE0; border-style: solid;border-width: 1px; background-color: #000000;">
		<strong>Images</strong></td>
	</tr>
	<tr>
		<td align="center" style="padding:5px; font-family: 'Times New Roman', Times, serif;font-size: 14pt;text-align:center; width: 734px; color: #2214B9;border-style: solid;border-width: 1px; background-color: #000000;">
	<table style="border: 1px solid #000000; width: 120">
<?php
$sql="SELECT * FROM pic";
$result=mysql_query($sql);
if ($result) {
	$cntr=mysql_num_rows($result)+1;
	$cnt=0;
	while($rows=mysql_fetch_array($result)){
		$cnt++;
		$name=$rows['Pic'];
		if ($cnt%5==1)echo "<tr>";
		echo"
		<td  width='120px'>
			<div style='border-style: outset; border-width: medium; border-color: #CCFFFF #CCFFFF #FFFFFF #CCFFFF; text-align: center; background-color: #FFFFDD;'>
			<a href='".$_SERVER['PHP_SELF']."?no=".$rows['ID']."'>
			<img alt='The picture is not available.' src='http://$name' height='100' width= '100'></a>
			<br>
			<a style='color:black' href='".$_SERVER['PHP_SELF']."?no=".$rows['ID']."'>".$rows['Title']."</a><br>			
			</div>
			</td>
		";
		if ($cnt%5==0)echo "</tr>";
	}	
	if (mysql_num_rows($result)==0) echo"<p style='text-align: center; color: #B21212;'><strong>No Picture Found</strong></p>";
}else {
	echo"<p style='text-align: center; color: #B21212;'><strong>No Picture Found</strong></p>";
}
?>
	</table>

		</td>
	</tr>
</table>
<br>
<form action="" method="post" enctype= "multipart/form-data" >
<table style="border: 1px solid #000000;width: 573px" align="center">
	<tr>
		<td style="font-family: 'Times New Roman', Times, serif;font-size: 17pt;text-align: center;width: 151px;color: #2214B9;border-style: solid;border-width: 1px;">
		<strong>Image NO.<?php echo $cntr?></strong></td>
		<td style="font-family: 'Times New Roman', Times, serif;font-size: 17pt;text-align: center;color: #2214B9;border-style: solid;border-width: 1px;">
			<table style="width: 100%">
				<tr>
					<td style="width: 68px; font-size: 14pt;">Title:</td>
					<td>
					
						<strong>
						<input name="title" style="width: 295px; font-size: 14pt; font-family: 'Times New Roman', Times, serif; font-weight: bold;"></strong>
					</td>
				</tr>
				<tr>
					<td style="width: 68; font-size: 14pt;" valign="top">
					Picture:</td>
					<td>
					<input name="picf" accept="image/*" style="font-size: 14pt; font-family: 'Times New Roman', Times, serif; width: 279px;" type="file"></td>
				</tr>
			</table>
		</td>
	</tr>
	
</table>
<p style="text-align: center">
<input name="Add" style="width: 118px; height: 63px; font-size: 30pt; font-family: 'Times New Roman', Times, serif; color: #19B024;" type="submit" value="Add"></p>
</form>
<div style="text-align: center; height: 15px">
	<font face="Tahoma"><a target="_blank" href="http://www.phpfreecode.com/">
	<span style="font-size: 8pt; text-decoration: none">PHP Free Code</span></a></font></div>
<a target='_blank' href='https://www.phpfreecode.com' style='font-size: 8pt; text-decoration: none'>Php Best Codes</a>                                                
                                            

Example:


About @m_k_amin

This user is pending a biography.

M

Comments


Here you can leave us commments. Let us know what you think about this code tutorial!

0 / 300

TRENDING POST
1
2
3
4
5
VISITORS
Online Users: 12
Recent Members: grkkid, Manaakividuinfo.com, karticksv, sava, tinatina
advertisement 2