Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

delete link after filename

9:31 am on Feb 24, 2009 (gmt 0)

New User

10+ Year Member

joined:July 4, 2007
posts: 18
votes: 0

I have used the PHP4 script here [webmasterworld.com...] to display a list of files in a folder, but can't figure out how to get a delete link working after each filename.

I know I have to use unlink, but I have no idea how to modify the script.

How is it done?

3:44 pm on Feb 24, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 13, 2009
votes: 0

paste the script here
5:39 pm on Feb 24, 2009 (gmt 0)

New User

10+ Year Member

joined:July 4, 2007
posts: 18
votes: 0

index.php is:

<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" id="submit" />
$dir = opendir('upload/');
echo '<ul>';

while ($read = readdir($dir))

if ($read!='.' && $read!='..')
echo '<li><a href="upload/'.$read.'">'.$read.'</a></li>';


echo '</ul>';


and upload.php (in same directory) is:

$target_path = "upload/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";

10:41 am on Feb 25, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 30, 2007
votes: 0

You can pass another link with a specific parameter to it so you can differentiate on the server end that you want to delete the file if that parameter is passed. So for example right after this:
echo '<li><a href="upload/'.$read.'">'.$read.'</a>';
you could add
echo ' <a href="delete_file_script.php?name=' . $read '">Delete '.$read.'</a></li>';

When the delete_file_script.php is invoked you check for the name parameter:
if( isset($_GET['name']) ) {
$filename = '/fullpath/upload/' . basename($_GET['name']);
if(file_exists($filename) ) {
Or you can process everything within the same script by changing the href parameter in the <a> tag.