Forum Moderators: coopster
<h2>Delete existing File</h2>
<p><select name="id"></p>
<option value="">-- Choose a file --</option>
<?php
echo "<form>";
//Looks into the directory and returns the files, no subdirectories
echo "<select name='yourfiles'>";
//The path to the file directory
$dirpath = "uploads";
$dh = opendir($dirpath);
while (false !== ($file = readdir($dh))) {
//Don't list subdirectories
if (!is_dir("$dirpath/$file")) {
//Truncate the file extension and capitalize the first letter
echo "<option value='$file'>" . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . '</option>';}
}
echo "</select></p>";
echo '<p><input type="submit" value="Delete" class="submit" />';
echo "</form>";
closedir($dh);
?>
<?php
$dirpath = "uploads";
$file_to_delete = $_POST['yourfiles']; //as named in your <select>
if ( unlink ($dirpath.'/'.$file_to_delete) ) {
echo $file_to_delete . " deleted.";
}
else {
echo "Error.";
}
?> [edited by: Matthew1980 at 8:19 pm (utc) on Jun 13, 2010]
The error I receive is: "Notice: Undefined index: yourfiles on line 6" (delete.php);
<?php
$dirpath = $_SERVER['DOCUMENT_ROOT'].'/'."uploads";
if (isset($_POST['yourfiles'])) {
$file_to_delete = $_POST['yourfiles'];
if ( unlink ($dirpath.'/'.$file_to_delete) ) {
echo $file_to_delete . " deleted.";
}
else {
echo "Delete Failed";
}
}
else {
echo "<p>No post variable found.</p>"
foreach ($_POST as $key => $value) {
echo "<p>key: $key value $value<p>";
}
?>