how to retrieve the image stored in the database via php? - php

I have created a database using phpmyadmim and to access it, I made a name.php file.
Accessing the registration number, name was easy, but the image retrieval is not so.
I stored the image via phpmyadmin using LongBlob as the type..but I'm not able to display it..
how to retrieve the image?
Please if someone can help,it would be high appreciated.
Thanks,
LJ
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PHYTOCHEMICAL DB</title>
<style type="text/css">
body {
font-family: Georgia, "Times New Roman",
Times, serif;
color: purple;
background-color: #EEEEEE}
</style>
</head>
<body>
<table width="800" border="" align="center">
<tr>
<td colspan="2" style="background-color:#FFA500;height:30px;width:700px">
<div> <img src="leaves.jpg" height="300" width="900"/> </div> <br/>
</td>
</tr>
<tr>
<td style="background-color:#FFD700;width:250px;">
<b>Menu</b><br>
<i> Home<br>
<i> Search by Database ID (DID) <br>
<i><a href="mw.php">Search by Molecular weight<br>
</td>
<td style="background color:#EEEEEE;height:500px;width:800px;">
<form action="" method="POST">
<p> Enter the name </p>
<input type="text" name="pname">
<input type="submit" value="submit" name="submit1">
<br>
</form>
<?php
$mysqli = new mysqli("localhost", "root", "osddosdd", "phychem");
if (mysqli_connect_errno()) {
printf("Connect failed:%s\n", mysqli_connect_error());
exit();
}
if (isset($_POST['submit1'])) {
$pname = $_POST['pname'];
$query = ("select * from pchem where name LIKE '$pname'");
$result = $mysqli->query($query);
echo 'The retrieved query is:';
echo "<table border='1'>
<tr>
<th>DID</th>
<th>Name</th>
<th>Molecular weight</th>
</tr>";
while ($row = $result->fetch_row()) {
echo '<tr>';
printf("<th>%d</th> <th> %s </th> <th> %2f </th>", $row[0], $row[1], $row[2]);
echo '</tr>';
echo '</table>';
}
}
$mysqli->close();
?>
</td>
</table>
</body>
</html>

don't store the images directly into the database as BLOBs. Just store them as files and only store the file names in the database.

I have googled and found some links that would solve you problem. Refer these links :
http://www.phpro.org/tutorials/Storing-Images-in-MySQL-with-PHP.html
http://www.coderslexicon.com/inserting-images-into-mysql-and-retrieving-them-using-php/
http://pujanpiya.com.np/?q=node/25

Related

display value from php table when clicking on a button on another page

Following is the table i created for displaying the Restaurant Name, Location and Menu for table owners.
Now each of the row for the column Menu have Button as values.
My table is ready with perfect values.
NOW MY PROBLEM IS HOW TO DO:-
Upon clicking the button corresponding to the each Restaurant, a new File(openmenu.php) will open and will echo the Restaurant Name, Mobile Number of that Restaurant and the menu.
But so far, on clicking every Button ,I can only display above entries of the Last row of the table. Help Me Out. I am new to php.
table.php
<?php
include 'nav.php';
$sql = 'SELECT * FROM owners';
$query = mysqli_query($con, $sql);
if (!$query) {
die ('SQL Error: ' . mysqli_error($con));
}
?>
<html>
<head>
<link rel = "stylesheet" type = "text/css" href = "css/style.css">
<style>
.data-table{
width: 1024px;
margin-left: 150px;
text-align:center;
border: 1px solid firebrick;
background-color: white;
}
td,th{
border: 1px solid firebrick; padding: 3px 2px 1px 1px;
}
</style>
</head>
<body>
<div class="container">
<article>
<table class="data-table">
<thead>
<tr>
<th>Restuarant Name</th>
<th>Location</th>
<th>Menu</th>
</tr>
<tr>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($query)){
$_SESSION['resphone'] = $row['resphone'];
$_SESSION['restaur'] = $row['restaur'];
echo '<tr>
<td>'.$row['restaur'].'</td>
<td>'.$row['loc'].'</td>
<td style="background-color:firebrick;"><form method="post" action="openmenu.php?id=$row[restaur]"><input value="<?php echo $restaur;?>" type="hidden">
<input type="submit" value="View"></form></td>
</tr>';
}
?>
</tbody>
</table>
</form>
</article>
</div>
</body>
</html>
openmenu.php
<?php
include('nav.php');
?>
<html>
<head>
<link rel="stylesheet" href="css/style.css">
<style>
table, td {
border: none;
text-align: center;
text-align-last: center;
}
</style>
</head>
<body>
<div class="container">
<article>
<form method="get" align="center" action="" class="formwrap" enctype='multipart/form-data'>
<h1><?php $restaur = $_SESSION['restaur'];
echo $restaur ;?></h1>
<h1>Call to Order:</h1>
<?php $resphone = $_SESSION['resphone'];
echo $resphone;
?>
<br>
<br>
<?php
$sql = "select img from owners where restaur ='$restaur'";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result);
$image_src2 = "upload/".$row['img'];
?>
<img src='<?php echo $image_src2; ?>' >
</form>
</article>
</div>
</body>
</html>
Issue 1
In this snippet you are setting the session variables resphone and restaur to the values of the store you are currently iterating over. Over and over again. That's why you're only ever getting the last store's information - it's the last things you set those variables to.
while ($row = mysqli_fetch_array($query)){
$_SESSION['resphone'] = $row['resphone'];
$_SESSION['restaur'] = $row['restaur'];
Issue 2
You should probably change the form method to get and discard the unused hidden input like so:
<form method="post" action="openmenu.php?id=<?=$row['restaur']?>">
<input type="submit" value="View">
</form>
Or more likely just change it a plain old a link:
View
Issue 3
You're completely ignoring store id requested in openmenu.php. You are using $_SESSION where you should be using $_REQUEST or $_GET. I'm not going to give an example of how you should do that. Instead, please refer to this answer before moving any further.
first you getting data from database & then use view button for openmenu.php but why u use this way
<form method="post" action="openmenu.php?id=$row[restaur]"><input value="<?php echo $restaur;?>" type="hidden"><input type="submit" value="View"></form>

Image from database is not displaying on webpage

so I have this blog that I am creating, but I am having issues with displaying an image from a database to the page itself. It only comes up with a broken image. The data does appear in the database however. It just doesn't display on the page.
Here is the image.php code (used to display the text and images):
<html>
<body>
<?php
//connect to database
//Name the variables
$host= "localhost";
//Localhost is the name of the computer that USBWebser has been loaded on
$username = "user";
$password = "pwd";
$database = "blog";
$dbh=mysql_connect("$host", "$username", "$password") or die('Could not connect:' .mysql_error());
//if cannot connect to database display error message
if(!$dbh)
{
echo mysql_error();
}
mysql_select_db("$database");
//get the id number of the row that the photo is located in and place it in $ano
$ano=$_GET['postID'];
//select the data and type for the photo identified by id
$sql="SELECT photo, phototype FROM blog where postID='$ano'";
//check if sql query can be executed
$r=mysql_query($sql, $dbh);
//if sql query can be executed
if($r)
{
//get the data from the query
$row=mysql_fetch_array($r);
//set the header information so that an image can be displayed
$type="Content-type: image/png" .$row['phototype'];
header($type);
//display the image
echo $row['photo'];
}
else
{
echo mysql_error();
}
?>
Here is the code for the main_menu.php (where I would like the image to appear)
<?PHP
//Name the variables
$host= "localhost";
//Localhost is the name of the computer that USBWebser has been loaded on
$username = "user";
$password = "pwd";
$database = "blog";
$mysqli=new mysqli($host, $username, $password, $database);
//Connect to Header
include "header.php";
?>
<?php
//Select fields from the posts table
$sql="SELECT postID, title, date, contents, rating, photo, phototype FROM posts";
//Place the data into a variable named $result
$result= $mysqli->query($sql);
if ($result->num_rows>0){
while ($row=$result->fetch_assoc()) {
?>
<br><table border="1" bordercolor="25dae3" width="53%"><th><font color="white">Title</th><th><font color="white">Date</th><th><font color="white">Contents</th><th><font color="white">Image</th><th><font color="white">Rating</th>
<tr><td width = "100" align="center"><font color="white">
<?php
echo $row["title"];
?>
</td>
<br><td width="100" align="center"><font color="white">
<?php
echo $row["date"];
?>
</td>
<br><td width="300" align="center"><font color="white">
<?php
echo $row["contents"];
?>
</td>
<br><td width="300" align="center"><font color="white">
<img src="<?php echo $row['photo']; ?>" width=300 height=300/>;
</td>
<br><td width="100" align="center"><font color="white">
<?php
echo $row["rating"];
?>
</td></tr></font>
<?php
}
} else {
//Display message that no data was present
echo "0 results";
}
//Close connection
$mysqli->close();
?>
The add_post.php
<html>
<header>
</header>
<body>
<?php
//Name the variables
$host = "localhost";
//Localhost is the name of the computer that USBWebserver has been loaded on
$username = "user";
$password = "pwd";
$database = "blog";
$mysqli=new mysqli($host, $username, $password, $database);
//Get variables from the form
$new_post_title=$_POST["newtitle"];
$new_post_date=$_POST["newdate"];
$new_post_contents=$_POST["newcontents"];
$new_post_rating=$_POST["newrating"];
$photo=addslashes(file_get_contents($_FILES["photo"]["tmp_name"]));
$imagesize=getimagesize($_FILES["photo"]["tmp_name"]);
//mime returns the image time eg. image/jpeg
$imagetype=$imagesize['mime'];
//Enable sql to read quotation marks within sentences
$new_post_title=addslashes($new_post_title);
$new_post_date=addslashes($new_post_date);
$new_post_contents=addslashes($new_post_contents);
$new_post_rating=addslashes($new_post_rating);
//Enter the new information into the posts table
$sql="INSERT INTO posts(postID, title, date, contents, rating, photo, phototype) VALUES (Null, '$new_post_title', '$new_post_date', '$new_post_contents', '$new_post_rating', '$photo', '$imagetype')";
//Run the query
$result=$mysqli->query($sql) or die (mysqli_error($mysqli));
if ($result) {
header ('location:main_menu.php');
}
else {
echo mysql_error();
}
?>
</body>
</html>
And the form to submit a post to the blog (add_new_post.php)
<HTML>
<style>
form {
border-opacity: 1.0 ;
display: incline-block;
text-align: center;
}
input[type=text]:focus, input[type=date]:focus {
width: 50%;
height: 20%;
border: 3px solid #00ffff;
}
body {
text-align: center;
padding-top: 50px;
}
</style>
<HEAD>
</HEAD>
<BODY><font color="white">
<br><br><br><H1 text-align="center">Add a New Post</H1>
<?php
//Connect To Header Page
include "header1.php";
//Connect To Database
include "dbconnect.php";
?>
<br>
<br>
<!-- <HR> Tag inserts a horizonal line across the page (horizontal rule)-->
<!-- <Form> Tag indicates that a form will be created -->
<!-- action indicates the file used to process the input when the submit button is pressed-->
<form enctype= "multipart/form-data" action="add_post.php" method = "POST">
Title: <br>
<!-- <input type> Tag indicates the type of input expected eg. text. Name = indicates the name given to the input-->
<input type="text" name="newtitle"><br>
Date: <br>
<input type="date" name="newdate"><br>
Contents: <br>
<input type="text" name="newcontents"><br>
Rating: <br>
<input type="text" name="newrating"><br>
Please Browse to where the photo is located:<br>
<input type = file name = "photo"><br>
<br>
<!-- Value indicates the text to be displayed. In this case, displayed on the button -->
<input type ="submit" value="Submit">
</form>
</BODY>
</HTML>
Any assistance on this issue would be appreciated.
Thanks
You are using php tag wrong
<br><td width="300" align="center"><font color="white">
<?php
echo "<img src="<?php echo $row['photo']; ?>" width=300 height=300/>";
?>
</td>
instead of above code use following code
<br><td width="300" align="center"><font color="white">
<img src="<?php echo $row['photo']; ?>" width=300 height=300/>
</td>

Inserting MySQL data into exsisting row

I need to insert a timestamp into table visitors column out after the user clicks on the sign-out button. The sign-out button already deletes rows in the table liveroster but its not updating rows in table visitors at column out with the timestamp.
mysql_query("UPDATE visitors SET out=test WHERE ID=" . $ID);
<?php
/*
Connection Stuff
*/
if (isset($_POST['ID'])) {
$ID = $_POST['ID'];
if (isset($_POST['delete_id'])) {
mysql_query("UPDATE visitors SET out=test WHERE ID=" . $ID);
mysql_query("DELETE FROM liveroster WHERE ID = " . $ID);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
#delete-post {
width: 100%;
margin: auto;
background-color: #999;
}
</style>
<script src="http://code.jquery.com/jquery-1.5.min.js"></script>
</head>
<body>
<div>
<table border cellpadding="3">
<tr>
<td></td>
<td><strong>Time</strong></td>
<td><strong>Name</strong></td>
<td><strong>Teacher</strong></td>
<td><strong>Reason</strong></td>
</tr>
<?php
$data = mysql_query("SELECT * FROM liveroster") or die(mysql_error());
while($info = mysql_fetch_array($data)){ ?>
<tr>
<th>
<form method="post" action="">
<input type="hidden" name="ID" value="<?php echo $info['ID']; ?>" />
<input type="submit" name="delete_id" value="Sign-Out" />
</form>
</th>
<td>
<?php echo $info['timestamp']; ?>
</td>
<td>
<?php echo $info['name']; ?>
</td>
<td>
<?php echo $info['teacher']; ?>
</td>
<td>
<?php echo $info['reason']; ?>
</td>
</tr>
<?php } ?>
</table>
</div>
</body>
</html>
Use PDO:
<?php
$dsn = "mysql:host=localhost;port=$port;dbname=$dbname";
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$dbh = new PDO($dsn, $username, $password, $options);
/*
Connection Stuff
*/
if (isset($_POST['ID'])) {
$ID = $_POST['ID'];
if (isset($_POST['delete_id'])) {
/* Update */
$count = $dbh->exec("UPDATE `visitors` SET `out` = 'test' WHERE `ID` = '" . $ID ."'");
/* Delete */
$count = $dbh->exec("DELETE FROM `liveroster` WHERE `ID` = '" . $ID ."'");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
#delete-post {
width: 100%;
margin: auto;
background-color: #999;
}
</style>
<script src="http://code.jquery.com/jquery-1.5.min.js"></script>
</head>
<body>
<div>
<table border cellpadding="3">
<tr>
<td></td>
<td><strong>Time</strong></td>
<td><strong>Name</strong></td>
<td><strong>Teacher</strong></td>
<td><strong>Reason</strong></td>
</tr>
<?php
$sql = "SELECT * FROM liveroster";
$sth = $dbh->prepare($sql);
$sth->execute();
$rows = $sth->fetchAll();
foreach($rows as $value){ ?>
<tr>
<th>
<form method="post" action="">
<input type="hidden" name="ID" value="<?php echo $value['ID']; ?>" />
<input type="submit" name="delete_id" value="Sign-Out" />
</form>
</th>
<td>
<?php echo $value['timestamp']; ?>
</td>
<td>
<?php echo $value['name']; ?>
</td>
<td>
<?php echo $value['teacher']; ?>
</td>
<td>
<?php echo $value['reason']; ?>
</td>
</tr>
<?php } ?>
</table>
</div>
</body>
</html>
You could create a column and update it using the "NOW()" function every time you click submit.
UPDATE visitors
SET out = test, getdate = NOW()
WHERE ID = $ID

How can I get it so at the top of my search results page it says the amount of results found?

I have a results page and I would like there to be a bit at the top of the page where it says how many results were returned. How do I do this?
My code is
<?php
if(strlen(trim($_POST['search'])) > 0) {
$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";
mysql_connect ("3", "", "");
mysql_select_db ("");
if (!empty($_POST["search_string"]))
{
}
$query = "SELECT name,location,msg FROM contact WHERE name LIKE '%$search%' AND
location LIKE '%$searchterm%'";
$result = mysql_query ($query);
if ($result) {
while ($row = mysql_fetch_array ($result)) { ?>
<center>
<table height="20" width="968" cellpadding="0" cellspacing="0">
<tr>
<td>
<table height="20" width="223" cellpadding="0" cellspacing="0">
<tr>
<td>
<font face="helvetica" size="2" color="#045FB4"><?php echo $row[0]; ?></font>
<hr size="1" color="#e6e6e6" width="100%"></hr>
</td>
</tr>
</table>
</td>
<td>
<table height="20" width="745" cellpadding="0" cellspacing="0">
<tr>
<td>
<font face="helvetica" size="2" color="black"><?php echo $row[1]; ?>
<?php echo $row[2]; ?></font>
<hr size="1" color="#e6e6e6" width="100%"></hr>
<td align="right">
<font face="helvetica" size="2" color="red">See More...</font>
<hr size="1" color="#e6e6e6" width="100%"></hr>
</td>
</tr>
</table>
</td>
</tr>
</table>
<?php
}
}
}
?>
</center>
THANKS!
James
If you're going to keep it to one page then mysql_num_rows() will do the trick and you're good to go. If you use pagination on the other hand, then your SELECT query will have a LIMIT clause and a second query can be constructed using COUNT(*) on the same tables with the same WHERE clause.
$total_query = "SELECT COUNT(*) FROM contact WHERE name LIKE '%$search%' AND
location LIKE '%$searchterm%'"
I’m not trying to compete with the other fine answers. I’m posting this as an answer because it’s too big for a comment.
Since you asked in a comment what could be done to improve your HTML, I refactored your code a bit just to illustrate some things you could do. I also included mysql_num_rows($result) mentioned by the others for the sake of completion.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sample</title>
<style type="text/css">
table {
font-family: helvetica;
font-size: small;
width: 968px;
}
td {
border-bottom: 1px solid #e6e6e6;
}
.name {
color:#045FB4;
}
.msg {
color:black;
}
.more {
color:red;
}
</style>
</head>
<body>
<?php
if(strlen(trim($_POST['search'])) > 0):
$search = mysql_real_escape_string($_POST["search"]);
$searchterm = mysql_real_escape_string($_POST["searchterm"]);
mysql_connect ("localhost", "root", "");
mysql_select_db ("sample");
if (!empty($_POST["search_string"]))
{
$search_string = mysql_real_escape_string($_POST["search_string"]);
// more code here
}
$query = "SELECT name,location,msg FROM contact
WHERE name LIKE '%$search%'
AND location LIKE '%$searchterm%'";
$result = mysql_query ($query);
if ($result):
$num_rows = mysql_num_rows($result);
?>
<p>Found <?php echo $num_rows; ?> results.</p>
<table>
<?php
while ($row = mysql_fetch_array ($result)):
?>
<tr>
<td class="name"><?php echo $row['name']; ?></td>
<td class="msg"><?php echo $row['location'], ' ', $row['msg']; ?></td>
<td class="more">See More...</td>
</tr>
<?php
endwhile;
endif;
endif;
?>
</table>
</body>
</html>
Normally, I would put the CSS in a separate file, but this is only an example.
Some things to notice:
There’s only one table
There’s no style information in the HTML
It uses mysql_real_escape_string. Ideally you'd also want to use prepared statements, but I’ll leave that as a personal exercise for you. :)
Even this can be improved quite a bit, but it's a start.
Try using mysql_num_rows($query). I am no PHP expert but, from memory, that should do the trick. Just echo this wherever you want it.
$num_rows = mysql_num_rows($result);
Check out the documentation: http://php.net/manual/en/function.mysql-num-rows.php
Also you can use SQL_CALC_FOUND_ROWS and FOUND_ROWS():
$query = "SELECT SQL_CALC_FOUND_ROWS name,location,msg FROM contact WHERE name LIKE '%$search%' AND location LIKE '%$searchterm%'";
$result = mysql_query($query);
if ($result)
{
$rs_count = mysql_query("SELECT FOUND_ROWS();");
$counted = (int)mysql_result($rs_count, 0);
}

delete selected images with the help of checkboxes

This is a page where users can edit their uploaded images. There is a checkbox near each image. I want to delete the selected images after user clicks the "delete selected images" button (each checkbox contains the image name as value). How can I do that?
<?php
session_start();
//////////////if user already logged in go to login.php/////////
if (isset($_SESSION['email'] )&& isset($_SESSION['password'] ))
{
} else{header( "Location: login.php" ); }
include('includes/config.php');
if (isset($_POST['esubmit']) ){
$checkbox=$_POST['delete'];
echo $checkbox;
}//main one
if (isset($_POST['esubmit']) ){
} else { $clickeditid=$_GET["id"];
$_SESSION['eid']= $clickeditid ;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css/css.css" rel="stylesheet" type="text/css" />
<style rel="stylesheet" type="text/css">
input {
border-style: solid;
border-color: #000000;
border-width: 1px;
background-color: #ffffff;
}
</style>
<script src="js/css_browser_selector.js" type="text/javascript"></script>
</head>
<body>
<?php
include('includes/topmenu.php');//top menu
echo '<br />';
include('includes/usermenu.php');///bcoz of this menu error occurs
////////////////////
include('includes/edit_img_menu.php');
?>
<form id="form1" name="form1" method="post" action="editimg.php?id=<?php echo $_SESSION['eid'];?>">
<table align="center" width="70%" cellspacing="0">
<tr>
<td colspan="3" align="center" bgcolor="#FFFFFF"></td>
</tr>
<tr>
<td colspan="3" align="center" bgcolor="#FFFFFF"><?php
$imgcheck=mysql_query("
SELECT *
FROM `images`
WHERE `deal_id` =$_SESSION[eid]
LIMIT 0 , 30
");
$numimgcheck=mysql_num_rows($imgcheck);
if($numimgcheck==0){echo '<span style=color:#ff0000; >No pictures uploaded</span>';}
while ($rowimg2= mysql_fetch_array($imgcheck)){
$imgname=$rowimg2['name'];
{
echo ' <a href="users/'.$_SESSION['userid'].'/images/'.$imgname.'" rel="lightbox[slide]" caption=".">';
}
{ echo '<img src="users/'.$_SESSION['userid'].'/images/thumbs/'.$imgname.'" border="0" />';}
{ echo '</a><input type="checkbox" name="delete" id="delete" value="'.$imgname.'">
';}
}
?></td>
</tr>
<tr>
<td colspan="3" align="center" bgcolor="#FFFFFF"> </td>
</tr>
<tr>
<td colspan="3" align="center" bgcolor="#95F8FD"><input name="esubmit" type="submit" class="red" id="esubmit" value="Delete selected images" /></td>
</tr>
<tr>
<td width="89"> </td>
<td colspan="2"> </td>
</tr>
</table>
</form>
</body>
</html>
You have to create a form to submit the data of the checked checkboxes and treate the submited result with the php code : if you want to put the php code on the same page, you can check if there is any data in the POST vars and create a SQL Query to delete the corresponding pictures.
You can find a very well documented tutorial here : http://sharemyphp.wordpress.com/2009/12/21/ajax-jquery-php-multiple-delete-item-with-check-box/
Regards,
Max
I use the below code to delete images in my admin panel. I saved the file as deletephoto.php. Note that this script works without login. You need to make some changes to fit your use case
<form method="post" action="deletepho.php">
<center>
<input type="submit" value="Delete" name="Delete">
</center>
<?php
\\lets assign the folder name in $title
\\You can assign any name
$title= "test";
\*$directory corresponds to whole path. Edit to your preference. (i assume u store your
images in directory named "images") */
$directory = "$title/images";
\\The array specifies the format of the files
$allowed_types=array('jpg','jpeg','gif','png');
$file_parts=array();
$ext='';
$title='';
$i=0;
$dir_handle = #opendir($directory) or die("There is an error with your image directory!");
while ($file = readdir($dir_handle))
{
if($file=='.' || $file == '..') continue;
$file_parts = explode('.',$file);
$ext = strtolower(array_pop($file_parts));
$title = implode('.',$file_parts);
$title = htmlspecialchars($title);
$nomargin='';
if(in_array($ext,$allowed_types))
{
if(($i+1)%4==0)
$nomargin='nomargin';
echo'
<div id="picture">
<img src="'.$directory.'/'.$file.'" width="150" height="150"><br>
Select <input type="checkbox" value="'.$directory.'/'.$file.'" name="imgfile[]">
\* I specified input name as an array . So that we can store in an array and delete it.*/
</div>';
}
$i++;
}
closedir($dir_handle);
\\Now we have the list of images within form elements
?>
</form>
Now here is the actual code to delete the photos. I saved it as deletepho.php.
$file = $_REQUEST['imgfile'];
$num_files=count($file);
for($i=0;$i<$num_files;$i++)
{
unlink ("$file[$i]");
}
echo "Images successfully deleted.Go <a href='deletephoto.php'>back</a>";

Categories