954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

delete image from folder using php

This is the code to delete image in a folder...

<?php
$path = "uploads";

if(isset($_POST['file']) && is_array($_POST['file']))
{
	foreach($_POST['file'] as $file)
	{	
		unlink($path . "/" . $file) or die("Failed to <strong class='highlight'>delete</strong> file");
	}
	header("location: " . $_SERVER['REQUEST_URI']); //redirect after deleting files so the user can refresh without that resending post info message
}
?>
<form name="form1" method="post">
<?php

$path = "uploads";
$dir_handle = @opendir($path) or die("Unable to open folder");

while (false !== ($file = readdir($dir_handle))) 
{

if($file == "index.php")
continue;
if($file == ".")
continue;
if($file == "..")
continue;

echo "<input type='CHECKBOX' name='file[]' value='$file'>";
echo "<img src='$file' alt='$file'>";

}
closedir($dir_handle);

?>
<input type="submit" name="Delete" value="Delete">
</form>

For the above code I'm unable to view the image.. any one help me please..

vijiglad
Newbie Poster
14 posts since Mar 2011
Reputation Points: 10
Solved Threads: 1
 

The $path is missing in the img tag in line 30.

smantscheff
Nearly a Posting Virtuoso
1,233 posts since Oct 2010
Reputation Points: 300
Solved Threads: 254
 

oh still unable to view images

vijiglad
Newbie Poster
14 posts since Mar 2011
Reputation Points: 10
Solved Threads: 1
 

Hai vijiglad

this is my code worked fine for me , i saved my images in the folder name 'images' . If u may more doubts mns see the attachment..

<?php
include 'config.php';
error_reporting(E_ALL ^ E_NOTICE);
$select = mysql_query("SELECT * from myimages");
if(isset($_POST['submit']))
{
	for($i=1;$i<=$_POST['hidden'];$i++)
	{
		
		$query2 = mysql_query("SELECT picture from myimages WHERE id = ' ".$_POST["hioddenid$i"]." ' ");
		while($file = mysql_fetch_array($query2))
		{
		$unlink = unlink('images/'.$file['picture']);
		if($unlink)
		{
		$query1 = mysql_query("DELETE from myimages WHERE id = ' ".$_POST["hioddenid$i"]." ' ");
		echo 'Deleted , You Will be Redirect';
		header("refresh : 3 imageview.php");
		}
		else
		{
		echo 'Not deleted';
		}
		}
	}
}
?>
<!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=iso-8859-1" />
<title>View Images</title>
</head>

<body>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<table align="center" cellspacing="15">
<tr>
	<th align="left">Action</th>
	<th align="left">Name</th>
	<th align="left">E Mail</th>
	<th align="left">Picture</th>
</tr>
<?php
$i = 0;
while($row = mysql_fetch_array($select))
{
$i = $i + 1;
?>
<tr>
	<td><input type="checkbox" name="hioddenid<?php echo $i; ?>" value="<?php echo $row['id']; ?>" /></td>
	<td><?php echo $row['name']; ?></td>
	<td><?php echo $row['email']; ?></td>
	<td><img src="images/<?php echo $row['picture'];?>" height="80" width="100"/></td>
	<input type="hidden" name="image<?php echo $i; ?>" value="<?php echo $row['picture'];?>" />
</tr>
<?php
}
?>
<tr>
	<input type="hidden" name="hidden" value="<?php echo $i; ?>" />
	<td colspan="3" align="center"><input type="submit" name="submit" value="Delete" /></td>
</tr>
</table>
</form>
</body>
</html>
Attachments Image_Delete.zip (414.36KB)
mohamedasif18
Junior Poster in Training
61 posts since Nov 2011
Reputation Points: 10
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: