取消链接/删除每个用户上传文件的文件(带复选框)

时间:2011-04-04 03:45:34

标签: php

您好我如何删除他在帐户中上传的每个用户的文件。 文件的位置是我的文件夹“uploader /”。 我试图把代码取消链接(“uploader / $ row1”); $ row1是每个上传文件的文件名。

代码:

<?php
**// Check if delete button active**
$delete1 = $_POST['delete'];
if(isset($delete1)) {
$checkboxID =$_POST['checkbox'];
for($i=0;$i<$count;$i++){
$del_id = $checkboxID[$i];
$sql = "DELETE FROM uploadedfiles WHERE fileid='$del_id'";
$result = mysql_query($sql);
}

if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=myfiles.php\">";
$sucdel = "Deletion Successful.";
}

}
mysql_close();
?>

复选框:

<tr>
    <td>&nbsp;</td>
    <td><center><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row0; ?>">
    </center></td>
    <td><center><?php echo $row1; ?></center></td>
    <td><center><?php echo "$row2 KB" ; ?></center></td>
    <td><center><?php echo $row3; ?></center></td>
    <td><center><?php echo $dlfile; }?></center></td>
    <td>&nbsp;</td>
  </tr>

显示所有用户上传文件的代码:

<?php
include "dbconnect.php";

$sql="SELECT * FROM uploadedfiles";
$result=mysql_query($sql);
$count = mysql_num_rows($result);
$query = mysql_query("SELECT * FROM uploadedfiles WHERE username='$usersess' ");
while ($row=mysql_fetch_array($query)) { 
$row0 = $row['fileid'];
$row1 = $row['name'];
$row2 = $row['size'];
$row3 = $row['type'];
$dlfile = "<a href='download.php?file=$row1'>Download</a>"; 

?>

一页就是全部。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

<?php
//loop through an array from check boxes. it wont delete the actual file tho, bad to use coz its possible to delete other peoples files :/
if (isset($_POST["checkbox"])){
    $delArr = $_POST["checkbox"];
    foreach ($delArr as $id) {
        mysql_query('DELETE from uploadedfiles where fileid ="'.$id.'"');
    }
}

//so get the users file list first then loop through and delete each one, then remove the rows

if (isset($_POST["checkbox"]) && isset($_POST['user'])){
    $result = mysql_query('SELECT name from uploadedfiles where username="'.mysql_real_escape_string($_POST['user']).'"');
    if(mysql_num_rows($result)>0){
        while ($row=mysql_fetch_array($result)) {
            unlink('path_to_userfiles/'.$row['uploadedfiles']);
        }
        mysql_query('DELETE FROM uploadedfiles WHERE username ="'.mysql_real_escape_string($_POST['user']).'"');
    }
}
?>