Hi everyone, I want to delete uploaded files from the server. here I am not using database please check my code and suggest me any changes

Del.php

<?php
$jpgdir = "uploads/";
$file=$_REQUEST['file1'];
echo $file;

//echo $jpgdir;
// Open a known directory, and proceed to read its contents
if (is_dir($jpgdir)) {
   if ($dh = opendir($jpgdir)) {
              unlink($jpgdir . "/" . $file);
       closedir($dh);
   }
}
?>

jpgview.php. From this file del.php will be called

<?php
$jpgdir = "uploads/jpg";

$dh = opendir($jpgdir);
echo "JPG FILES";

print("<form name=del action=\"del.php\">");
print("<table border=2>");
while (($file = readdir($dh)) !== false) {
	if($file!='.' && $file!='..')
	{
	print( "<tr><td>");
echo "<A HREF=\"$jpgdir/$file\"><IMG src=\"$jpgdir/$file\"/ height=70 width=70></A><BR>\n";
	print("</td><td>");
		echo "<input type=\"submit\" name=\"del\" value=\"Delete\" id=\"$file\">";
print("</td></tr>");

	}
}
print("</table>");
print("</form>");
closedir($dh);
?>

My problem is I am unable to get the buttons corresponding image name and send it to del.php


Thanx in advance

Try this.

This lists all the images in the specified folder.

<?php
$jpgdir = "./uploads/jpg/";

$dh = opendir($jpgdir);
echo "JPG FILES";

print("<table border=2>");
while (($file = readdir($dh)) !== false) {
	if($file!='.' && $file!='..')
	{
		print( "<tr><td>");
		print("<form name=del method=post action=\"del.php\">");
		echo "<A HREF=\"$jpgdir/$file\"><IMG src=\"$jpgdir/$file\"/ height=70 width=70></A><BR>\n";
		echo "<input type=\"hidden\" name=\"path\" value=\"$jpgdir/$file\">";
		print("</td><td>");
		echo "<input type=\"submit\" name=\"del\" value=\"Delete\">";
		print("</td></tr>");
		print("</form>");
	}
}
print("</table>");
closedir($dh);
?>

This code deletes that image and sends the user back to the image listing page.

<?php
$jpgdir = "./uploads/jpg/";;
$file=$_REQUEST['path'];

// Open a known directory, and proceed to read its contents
if (is_dir($jpgdir)) {
   if ($dh = opendir($jpgdir)) {
              unlink($file);
       closedir($dh);
   }
}
header("location: jpgview.php");
?>

This works.

Cheers,
Naveen

This question has already been answered. Start a new discussion instead.