Click to See Complete Forum and Search --> : Need to delete file (unlink) from directory listing


fjhughes
03-20-2008, 08:40 PM
Someone asked me how to delete a file using PHP. They have done a directory listing to list all the files in a directory. Users can download the files they want by clicking on the file. How could I put a "Delete" link beside each file name and be sure that it will specifically delete THAT file?

Thanks

-fjh

zenag
03-21-2008, 05:11 AM
<?
if(isset($_POST['upload']))
{
$uploadDir="filefolderpath/";
$fileName = $_FILES['file']['name'];

$tmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileType = $_FILES['files']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;

}
}
if($_GET)
{
$var=$_GET["var"];
$path="filefolderpath/".$var;

$status=unlink($path);

}?>

<!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>Untitled Document</title>

</head>

<body>
<form action="" method="post" onsubmit="return uploadima();" enctype="multipart/form-data">
<table>
<tr><td><input type="file" name="file" id="file" /> </td>
</tr>
<tr><td><input type="submit" name="upload" value="submit" /></td></tr>
</table></form><form action="" method="get"><table>
<tr><td><? $count = "0";
$getDir = opendir("filefolderpath/");
while($filename = readdir($getDir)) {
if ($filename[0]!= "." && $filename[0]!= ".." ) {
echo "<a href='filefolderpath/".$filename."'>$filename</a></br>";?>
<a href="img.php?var=<? echo $filename;?>">delete</a><br />
<?
$count++;
}
} ?>
</td></tr>
</table>
</form>
</body>
</html>

fjhughes
03-21-2008, 11:21 AM
What is in the img.php file?

Thanks,

fjh

zenag
03-22-2008, 01:51 AM
i,ve linked it to the same file...

write it as
<a href="?var=<? echo $filename;?>">delete</a><br />

fjhughes
03-22-2008, 11:08 PM
That worked perfect!

Thanks!

-fjh