I am using PHP to retrieve a list of files from a directory with a checkbox next to each of them. When the user puts a check in one or more of the checkboxes and clicks a submit button on the form, I want the files with a check in the box to be deleted from the directory.
My logic behind getting this to work is wrong somewhere. I'm not really sure where it is. Does anyone have ideas? Any and all help would be greatly appreciated. Thanks!
Here is the code (that I found somewhere online) that lists the files in the specified directroy and puts a checkbox next to each file. This works well in displaying the directory contents on the screen:
[code=php]$audio_upc = $_GET['upc'];
//define the path as relative
$path = "../Lounge/audio/$audio_upc";
//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");
//running the while loop
while ($file = readdir($dir_handle))
{
if($file!="." && $file!="..")
echo "<input type=\"checkbox\" name=\"delete\" value=\"$file\">$file<br>";
}
//closing the directory
closedir($dir_handle);
print "</td></tr>";[/code]
This is the code that I am currently using to delete the file(s) from the directory, but it is not working. When the code executes I don't get any errors or anything, but the files are not deleted from the directory and the "print $path;" and "print $file;" do not return anything. Maybe my whole problem is a variable scope issue??? I'm not sure.
[code=php]$audio_dir_new = $_POST[upc];
global $path;
global $file;
print $path;
print $file;
if ($_POST['delete'] == '$file') {
$delete_audio = "$path/$file";
unlink($delete_audio);
}[/code]