I have a dropdown list populated with files pulled from a directory using the PHP listed below and am trying to figure out how I can delete them using a delete button in the form when they're selected.
EDIT Unlink function error resolved.
Still getting error when trying to delete: Error.array(1) { ["Action"]=> string(6) "Delete" }
<input type="hidden" name="Action" value="EDIT" /><input type="hidden" name="Selection" id="Selection" value="-1"><div>Below is the list of your saved codes. To edit your codes, select it from the list.</div>
<select size="1" name="CodeList" id="CodeList">
<?php
$directory = $directory = 'users/' . $_SESSION['username'];
$filesContents = Array();
$files = scandir( $directory ) ;
foreach( $files as $file )
{
if ( ! is_dir( $file ) )
{
$filesContents[$file] = file_get_contents($directory , $file);
echo '<option value="'. $file .'">' . $file . '</option>';
}
}
?>
delete php script.
<?php
session_start();
$directory = $directory = $_SERVER['DOCUMENT_ROOT'] . '/users/' . $_SESSION['username'];
$file_to_delete = $_POST['CodeList'];
if(is_file($directory .'/'. $file_to_delete) && is_writable($directory .'/'. $file_to_delete)) {
if ( unlink ($directory.'/'.$file_to_delete) ) {
echo $file_to_delete . " deleted.";
} else {
echo "Error.";
}
}
?>
<?php var_dump($_POST) ?>