/    Sign up×
Community /Pin to ProfileBookmark

Need to delete file (unlink) from directory listing

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

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@zenagMar 21.2008 — <?

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>
Copy linkTweet thisAlerts:
@fjhughesauthorMar 21.2008 — What is in the img.php file?

Thanks,

fjh
Copy linkTweet thisAlerts:
@zenagMar 22.2008 — i,ve linked it to the same file...

write it as

<a href="?var=<? echo $filename;?>">delete</a><br />
Copy linkTweet thisAlerts:
@fjhughesauthorMar 23.2008 — That worked perfect!

Thanks!

-fjh
×

Success!

Help @fjhughes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...