如何删除本地主机上的上传文件以及用户帐户在Php

时间:2015-01-20 10:56:56

标签: php forms mysqli

这是注册页面php代码,当用户注册时,他必须在txt ||上传文件doc格式。现在我希望每当用户通过点击删除按钮删除他在delete.php上的帐户时,他上传的文件会在我的localhost wamp www / govt / document"也应该删除用户帐户,但现在只有帐户正在删除,文件在那里,我想删除那些登录的特定用户上传的文件。     

if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $password= $_POST['password'];
    $day = $_POST['dob'];
    $month=$_POST['dob2'];
    $year = $_POST['dob3'];
    if(isset($_POST['gender'])){
    $gender = $_POST['gender'];
    }
    if(isset($_FILES['upload'])){
    $filename = $_FILES['upload']['name'];
    $filetmp = $_FILES['upload']['tmp_name'];
    $filetype = $_FILES['upload']['type'];
    $fileError= $_FILES['upload']['error'];
    $filesize= $_FILES['upload']['size'];
    }
$max=1000000;
if(!empty($name) && !empty($password) && !empty($day) && !empty($month) && !empty($year) && isset($gender)){
if(!empty($password)){
$password=sha1($password);
if(!empty($name)){
$query=mysqli_query($conn,"SELECT * FROM govtup WHERE name='$name'");
$count=mysqli_num_rows($query);
if ($count==1) {
echo "This name is already registered please choose another username";
}else{
if($fileError==0){
if($filesize<=$max){
if (($filetype=="text/plain") || ($filetype=="application/msword")){
move_uploaded_file($filetmp, "document/".rand(100,9999999).time()."$filename");
$query2=mysqli_query($conn,"INSERT INTO govtup(name,password,day,month,year,gender) VALUES ('$name','$password','$day','$month','$year','$gender')");
if($query2){
unset($name);
header("Location: login.php");
echo "Successfully registered";
}else{
echo "not registered";
}
}else{
echo "only txt and doc file allowed";
}
}else{
echo "The file is too big please choose filesize less than 1mb";
}
}else{
echo "please choose a txt or doc file to upload";
}
}
}
}
}else{
echo "Please fill all the field";
}
}

?>

Delete.php代码 这是从mysql phpmyadmin删除用户帐户详细信息的delete.php代码,我希望当用户点击删除帐户按钮时,那么现在登录的用户上传的文件应该与用户详细信息一起删除。

<?php 

session_start();
require_once("inc/connection.php");
if(empty($_SESSION['usersession'])){
header("Location: login.php");
}
if(isset($_SESSION['msg'])){
echo $_SESSION['msg'];
unset($_SESSION['msg']);
}


?>
<!DOCTYPE html>
<html>
<head>
<title>REGISTER HERE</title>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
</head>
<body>
<div id="wrapper">
<?php 
if(isset($_POST['delete'])){
    $id=mysqli_real_escape_string($conn,$_SESSION['usersession']['id']);
    $query=mysqli_query($conn,"DELETE FROM govtup WHERE id='$id'");
    if($query){
        header("Location: login.php");
    }else{
        echo "Unable to proccess your request";
    }
    $_SESSION['msg']="Your Account Has  Been Deleted";
    unset($_SESSION['usersession']);
}

?>
<form enctype="multipart/form-data" method="post" action="" id="form">
<label class="lb">USERNAME</label><p>
<input type="text" name="name" class="int"  value="<?php echo $_SESSION['usersession']['name'];?>" readonly/><p>

<label class="lb">D.O.B</label><p>
<input type="text" name="dob" value="<?php echo $_SESSION['usersession']['day']."-". $_SESSION['usersession']['month']."-".$_SESSION['usersession']['year']; ?>" readonly/>

<P>
<label class="lb">GENDER</label><p>
<input type="text" name="gender" value="<?php echo $_SESSION['usersession']['gender'];?>" readonly/>
<p>
<input type="submit" name="delete" id="dbtn" value="delete account">
</form>
</div>
<script type="text/javascript" src="inc/jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("#dbtn").click(function(e){
            if(!confirm("Are you sure you want to delete your account")){
                e.preventDefault();
                return false
            }
            return true;

        })

    });
</script>
</body>
</html>

4 个答案:

答案 0 :(得分:1)

您必须在数据库中创建新列存储 文件名。在这里,我修改您的插入查询

$query2=mysqli_query($conn,"INSERT INTO govtup(name,password,day,month,year,gender,file) VALUES ('$name','$password','$day','$month','$year','$gender','$filename')");

删除部分

if(isset($_POST['delete'])){
    $id=mysqli_real_escape_string($conn,$_SESSION['usersession']['id']);

    $select = mysqli_fetch_assoc(mysqli_query($conn,"SELECT file FROM govtup WHERE id='$id'")); //Fetch the file which is associated with this account

    unlink('YourFolderPath/'.$select['file']); //Deleting the file

    $query=mysqli_query($conn,"DELETE FROM govtup WHERE id='$id'");
    if($query){
        header("Location: login.php");
    }else{
        echo "Unable to proccess your request";
    }
    $_SESSION['msg']="Your Account Has  Been Deleted";
    unset($_SESSION['usersession']);
}

答案 1 :(得分:0)

如果我没有记错的话,您不会将信息,哪些文件属于任何地方的相应用户。 如果您将文件名与用户一起存储,则可以检索文件名并unlink

答案 2 :(得分:0)

创建具有唯一ID的文件夹。并将其分配给用户。 如果用户删除了他的帐户删除相关文件夹。

rmdir($dirname) 

答案 3 :(得分:0)

对于你的删除问题首先检查你的$选择你的回报,并通过回声检查你的图像路径。 DB和文件夹中的文件名应相同。

首先,对于更新问题,您将检查用户是否更改了图像,然后从DB获取该用户的文件名并以相同方式取消链接。如果用户没有更改图像,则无需取消链接该图像,也不需要更新DB列