如何删除本地主机上的上传文件以及 Php 中的用户帐户
How To Delete Uploaded File On Local Host Along With User Account In Php
这是注册页面 php 代码,当用户注册时,他必须上传 txt 文件 || doc format.Now 我希望每当用户通过单击删除按钮删除他在 delete.php 上的帐户时,他在 "document folder in my 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代码
这是 delete.php 代码,它从 mysql phpmyadmin 中删除用户帐户详细信息,我希望当用户单击删除帐户按钮时,然后是正确登录的用户上传的文件现在应该连同用户详细信息一起删除。
<?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>
如果我没记错的话,您没有在任何地方存储信息,哪些文件属于各个用户。
如果您将文件名与用户一起存储,则可以检索文件名并 unlink
它。
创建具有唯一性的文件夹 id.And 将其分配给用户。
如果用户删除他的帐户,则删除与此关联的文件夹。
rmdir($dirname)
您必须在 DB 中创建一个新列,它将存储 文件名 .我在这里 修改 您的 插入查询 。
$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']);
}
对于您的删除问题,首先检查您的 $select 您的 return 并通过回显检查您的图像路径。数据库中的文件名和文件夹中的文件名应该相同。
对于更新问题,首先您将检查用户是否更改了图像,如果他更改了图像,然后从数据库中获取该用户的文件名并以相同的方式取消链接。如果用户没有更改图像,那么您不需要取消链接该图像,也不需要更新数据库列
这是注册页面 php 代码,当用户注册时,他必须上传 txt 文件 || doc format.Now 我希望每当用户通过单击删除按钮删除他在 delete.php 上的帐户时,他在 "document folder in my 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代码 这是 delete.php 代码,它从 mysql phpmyadmin 中删除用户帐户详细信息,我希望当用户单击删除帐户按钮时,然后是正确登录的用户上传的文件现在应该连同用户详细信息一起删除。
<?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>
如果我没记错的话,您没有在任何地方存储信息,哪些文件属于各个用户。
如果您将文件名与用户一起存储,则可以检索文件名并 unlink
它。
创建具有唯一性的文件夹 id.And 将其分配给用户。 如果用户删除他的帐户,则删除与此关联的文件夹。
rmdir($dirname)
您必须在 DB 中创建一个新列,它将存储 文件名 .我在这里 修改 您的 插入查询 。
$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']);
}
对于您的删除问题,首先检查您的 $select 您的 return 并通过回显检查您的图像路径。数据库中的文件名和文件夹中的文件名应该相同。
对于更新问题,首先您将检查用户是否更改了图像,如果他更改了图像,然后从数据库中获取该用户的文件名并以相同的方式取消链接。如果用户没有更改图像,那么您不需要取消链接该图像,也不需要更新数据库列