Delete uploaded file

Hi all,

I wanted to know how to create an image link so that it deletes an uploaded file.
The line I’m concern about is below:


echo "<a href=\\"dl.php?file=".$file."\\">".$file."</a><img onclick=".$unlink." src=\\"../deleteCross.gif\\" /><br />";

I’ve tried using a hyperlink but no luck so I tried onclick, however still no luck.

If you need any more code let me know

Thanks,

puzzled19

You could just simply use straight PHP and do something like this

echo "<a href=\\"dl.php?file=".$file."\\">".$file."</a><a href=\\"dl.php?file=".$file."&amp;delete=true\\"><img src=\\"../deleteCross.gif\\" border=\\"\\" /></a><br />";

then in your dl.php file have

if (isset($_GET['delete']) && $_GET['delete'] == true && isset($_GET['file']) && !empty($_GET['file'])){
    $directory = './files/';
    if (@unlink($directory.$_GET['file'])){
        header('Location: index.php');
    } else {
        die('An error occured');
    }
}

Hope that helps some what

 echo '<a href="dl.php?file='.$file.'"><img src="../deleteCross.gif" /></a><br />'; 

Use the above code for the delete link and write the code in dl.php file to delete the image.

use unlink() function to delete it.

Thanks for the help. I tried both your suggestions but I got nothing. This is where I am now:

The link I’m using:


echo "<a href=\\"dl.php?file=".$file."\\">".$file."</a>&nbsp;<a href=\\"dl1.php?file=".$file."\\"><img src=\\"../deleteCross.gif\\" /></a><br />";

I decided to put the unlink in a file called dl1.php because it wouldn’t work in the original dl.php. Below is dl1.php


<?php
if(isset($_GET['file']) && basename($_GET['file']) == $_GET['file'])
{
	$filename = $_GET['file'];
	$path = chdir('upload/'.$filename);
	if(file_exists($path) && is_readable($path))
	{
			unlink($path);
			echo "The file: ".$filename." has been deleted.";
			echo "Please click <a href=\\"index.php\\">here</a> to return";
			else
			{
				echo "not deleteing";
			}
	}
	else { die("error here"); }
}
else
{
	$filename = NULL;
	echo "no file";
}
$err = 'The link you are requesting is unavailable';

?>

The link now goes through but the file does not delete and I get no messages output to the screen. Any other tips you could give me? Thanks

ok I think after a days worth of work :confused: I’ve finally got the script that works with a confirmation aswell:

the dl.php:


if(isset($_GET['delete']) && $_GET['delete'] == true)
		{
			unlink($path);
			echo "The file: ".$file." has been deleted.";
			echo "Please click <a href=\\"../admin/admin.php\\">here</a>";			
		}

the link:


echo "<a href=\\"dl.php?file=".$file."\\">".$file."</a>&nbsp;<a href=\\"dl.php?file=".$file."&amp;delete=true\\" onclick=\\"return confirm('Are you sure you want to delete ".$file."?')\\"><img src=\\"../deleteCross.gif\\" /></a><br />";

Thank you for all your help