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'><br />";

}
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..

Recommended Answers

All 4 Replies

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

oh still unable to view images

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>
commented: Good. +0

<snipped> this is the working code images view with delete option.

<?php

echo scanDirectoryImages("uploads");

/**
 * Recursively search through directory for images and display them
 * 
 * @param  array  $exts
 * @param  string $directory
 * @return string
 */
function scanDirectoryImages($directory, array $exts = array('jpeg', 'jpg', 'gif', 'png'))
{
    if (substr($directory, -1) == 'uploads/') {
        $directory = substr($directory, 0, -1);
    }
    $html = '';
    if (
        is_readable($directory)
        && (file_exists($directory) || is_dir($directory))
    ) {
        $directoryList = opendir($directory);
        while($file = readdir($directoryList)) {
            if ($file != '.' && $file != '..') {
                $path = $directory . '/' . $file;
                if (is_readable($path)) {
                    if (is_dir($path)) {
                        return scanDirectoryImages($path, $exts);
                    }
                    if (
                        is_file($path)
                        && in_array(end(explode('.', end(explode('/', $path)))), $exts)
                    ) {
                        $html .= '<a href="' . $path . '"><img src="' . $path
                            . '" style="max-height:100px;max-width:100px" /></a>';
                    }
                }
            }
        }
        closedir($directoryList);
    }
    return $html;
}
?>
<?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'>";



}
closedir($dir_handle);
?>
<input type="submit" name="Delete" value="Delete">
</form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.