I'm building a simple PHP file manager, and I need help getting files to be deleted.
Basically, how can I check to see if .filename checkbox
is checked, where .filename is the class of the parent <tr>
, as well as the name of the file, then if it is, when you 开发者_运维百科click <input type="submit" name="delete" value="Delete Selected Files" />
, delete the selected files from the /uploads/
directory.
I'm not using any kind of database or anything, just password protected areas on the site. I realize this isn't the most secure way to do things.
I already have a script that populates a table like this:
<table>
<tr>
<td> </td>
<td>File Name:</td>
<td>File Size:</td>
</tr>
<tr class="filemane">
<td><input type="chekbox" /></td>
<td><a href="/uploads/file-name.ext" target="_blank">file-name.ext</a></td>
<td>10</td>
</tr>
</table>
<table>
<tr>
<td><input type="submit" name="upload" value="Upload Files" /></td>
<td><input type="submit" name="delete" value="Delete Selected Files" /></td>
</tr>
</table>
So I just need to know how to check which checkboxes are checked, and delete the related files from the /uploads/
directory.
You cam use: HTML:
<input type="chekbox" name="check_files[]" value="unique_file_id1"/>
<input type="chekbox" name="check_files[]" value="unique_file_id2"/>
PHP:
if (!emptY($_POST['check_files'])) {
foreach ($_POST['check_files'] as $checked_file) {
if(file_exists("path" . $checked_file))) {
unlink("path" . $checked_file);
}
//other logic
}
}
<?php
if (isset($_POST['delete']))
{
for($i=0;$i<count($_POST['check_files']);$i++)
{
if (file_exists($_POST['check_files'][$i]))
{
unlink($_POST['check_files'][$i]);
}
}
}
?>
Maybe try using JavaScript and Ajax requests:
var chkBox, destroy, fileName;
while(chkBox = document.querySelector('.filename input:checked')){
fileName = chkBox.parentNode.parentNode.querySelector('a').href;
alert('Removing file: ' + fileName); // send ajax request etc
destroy = chkBox.parentNode.parentNode;
destroy.parentNode.removeChild(destroy);
}
Working example: http://jsfiddle.net/E5WWN/3/
精彩评论