Hi guys,

ok so here's the deal: currently I have this PHP code that allows someone to see every files that are in the directory. I would also like for them to be able to delete any files he wants.

Here's my PHP code:

<?php
 // open this directory 
$myDirectory = opendir(".");

// get each entry
while($entryName = readdir($myDirectory)) {
	$dirArray[] = $entryName;
}

// close directory
closedir($myDirectory);

//	count elements in array
$indexCount	= count($dirArray);

// sort 'em
sort($dirArray);

// print 'em
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");
print("<TR><TH>Nom</TH><th>Type</th><th>Grosseur</th></TR>\n");
// loop through the array of files and print them all
for($index=0; $index < $indexCount; $index++) {
        if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
		print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
		print("<td>");
		print(filetype($dirArray[$index]));
		print("</td>");
		print("<td>");
		print(filesize($dirArray[$index]));
		print("</td>");
		print("</TR>\n");
	}
}
print("</TABLE>\n");
?>

Does anyone know how I can add a link to the table that allows the person to delete the file next to it? Thank you very much your help is greatly appreciated!

You have it figured out pretty good by yourself, but to help you in the right direction, think of something like this:

$filename = "D:/some/folder/example.txt";
$text = "<a href='delete.php?file=".$file."'>Delete ".$file."</a>";

And apply that to your own script (thus making a new page called 'delete.php' that retrieves the filename from the URL using GET and deletes it)

~G

You have it figured out pretty good by yourself, but to help you in the right direction, think of something like this:

$filename = "D:/some/folder/example.txt";
$text = "<a href='delete.php?file=".$file."'>Delete ".$file."</a>";

And apply that to your own script (thus making a new page called 'delete.php' that retrieves the filename from the URL using GET and deletes it)

~G

I'm not quite good enough with PHP to do that... Do you think you could show me how to put in the table created by the script in my previous post?

Also, what script do I put on the delete.php?

EDIT: Also people are going to upload files so I will not be able to know the names... Is there a way for it to retrieve the name according to the file in the directory list?

Anyone?

Add:

$file = urlencode($dirArray[$index]);
print("<td><a href=\"delete.php?file=$file\">Delete $dirArray[$index]</a></td>");

In delete.php something like this:

$file = $_GET['file'];
unlink($file);

Add:

$file = urlencode($dirArray[$index]);
print("<td><a href=\"delete.php?file=$file\">Delete $dirArray[$index]</a></td>");

In delete.php something like this:

$file = $_GET['file'];
unlink($file);

It works perfectly!!! Thank you very much!

Just note that I had the change the href from $file to $dirArray[$index] in order for it to work!

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.