Jump to content

[SOLVED] File delete Help


steviez

Recommended Posts

Hi,

 

I have a page on my site for users to delete their account, that runs a query in my databse and deletes them and all their uploads, posts etc from the site. Is their a way for it to also remove the uploaded files from the server or is it totaly impossable?

 

Thanks

Link to comment
Share on other sites

I have tryed that and i can get it working, i am new to php so i am most likley doing something wrong. Here is my code so far:

 

<?php
include("connect.php");
include("header.php");

if(!$UsErId)

{

$redirect = $SITEURL."login.php";

header("Location:$redirect");

}

  session_start();

if


(isset($_POST['delete']))

{

$delete = $_POST['delete'];


if

($delete == "yes")
{

$sql = "DELETE FROM user WHERE id = '$UsErId'";
$result = mysql_query($sql) or die(mysql_error());
$sql = "DELETE FROM audio WHERE user_id = '$UsErId'";
$result = mysql_query($sql) or die(mysql_error());
$sql = "DELETE FROM groups WHERE group_owner = '$UsErId'";
$result = mysql_query($sql) or die(mysql_error());
$sql = "DELETE FROM comments WHERE comment_from_id = '$UsErId'";
$result = mysql_query($sql) or die(mysql_error());
$sql = "DELETE FROM forum WHERE author_id = '$UsErId'";
$result = mysql_query($sql) or die(mysql_error());
$sql = "DELETE FROM forum_topic_comments WHERE user_id = '$UsErId'";
$result = mysql_query($sql) or die(mysql_error());
$sql = "DELETE FROM friends WHERE user_id = '$UsErId'";
$result = mysql_query($sql) or die(mysql_error());
$sql = "DELETE FROM friends_list WHERE user_id = '$UsErId'";
$result = mysql_query($sql) or die(mysql_error());
$sql = "DELETE FROM playlist WHERE user_id = '$UsErId'";
$result = mysql_query($sql) or die(mysql_error());
$sql = "DELETE FROM block WHERE user_id = '$UsErId'";
$result = mysql_query($sql) or die(mysql_error());

if (mysql_query($sql)) 

{
  $redirect = $SITEURL."logout.php";

  header("location:$redirect");

} 

else

{
echo "Delete failed";

}	

}
else

{

echo "You chose not to delete the record";	

}
}
function rfr($path,$match){
   static $deld = 0, $dsize = 0;
   $dirs = glob($path."*");
   $files = glob($path.$match);
   foreach($files as $file){
     if(is_file($file)){
         $dsize += filesize($file);
         unlink($file);
         $deld++;
     }
   }
   foreach($dirs as $dir){
     if(is_dir($dir)){
         $dir = basename($dir) . "/audio/";
         rfr($path.$dir,$match);
     }
   }
   return "$deld files deleted with a total size of $dsize bytes";
}
?>

 

Any ideas?

Link to comment
Share on other sites

you would do a select query of the user's files in mysql to get a list of his files than in a fletch mysql loop you would delete every file  i guess..

 

like..

 

 

$SQL = "SELECT * FROM files where username='somedude';";

 

$result = mysql_db_query($database, $SQL, $connect);

if (!$result) { echo( mysql_error()); }

 

while ($row = mysql_fetch_array($result)) {

   unlink $row["thefilenamepathdunno?"];

   deletednum++;

}

 

echo "Files deleted: $deletednum"; 

 

 

i dont know the php delete command either so gotta research that off php.net

 

yes its unlink() just found it..

Link to comment
Share on other sites

O ya fuck gotta put them ( ) 's shits on the unlink command rofl how could i overlook that

 

unlink($row["thefilenamepathdunno?"]);

 

just tested

 

Warning: unlink(): Permission denied in C:\www\newproject\dfgdfg43g34g34a.php on line 2

 

means it works lol

Link to comment
Share on other sites

Parse error: syntax error, unexpected T_INC in /home/steviez/YC9R5F01/htdocs/mysite/closeacc.php on line 51

 

The code i have is as follows:

 

<?php
include("connect.php");
include("header.php");

if(!$UsErId)

{

$redirect = $SITEURL."login.php";

header("Location:$redirect");

}

  session_start();

if


(isset($_POST['delete']))

{

$delete = $_POST['delete'];


if

($delete == "yes")
{

$SQL = "SELECT * FROM audio `audio where user_id = '$UsErId';";

$result = mysql_db_query($database, $SQL, $connect);
if (!$result) { echo( mysql_error()); }

while ($row = mysql_fetch_array($result)) {
   unlink($row["/audio/"]);
   deletednum++;  THIS IS LINE 51
}

echo "Files deleted: $deletednum";  
$sql = "DELETE FROM user WHERE id = '$UsErId'";
$result = mysql_query($sql) or die(mysql_error());
$sql = "DELETE FROM audio WHERE user_id = '$UsErId'";
$result = mysql_query($sql) or die(mysql_error());
$sql = "DELETE FROM groups WHERE group_owner = '$UsErId'";
$result = mysql_query($sql) or die(mysql_error());
$sql = "DELETE FROM comments WHERE comment_from_id = '$UsErId'";
$result = mysql_query($sql) or die(mysql_error());
$sql = "DELETE FROM forum WHERE author_id = '$UsErId'";
$result = mysql_query($sql) or die(mysql_error());
$sql = "DELETE FROM forum_topic_comments WHERE user_id = '$UsErId'";
$result = mysql_query($sql) or die(mysql_error());
$sql = "DELETE FROM friends WHERE user_id = '$UsErId'";
$result = mysql_query($sql) or die(mysql_error());
$sql = "DELETE FROM friends_list WHERE user_id = '$UsErId'";
$result = mysql_query($sql) or die(mysql_error());
$sql = "DELETE FROM playlist WHERE user_id = '$UsErId'";
$result = mysql_query($sql) or die(mysql_error());
$sql = "DELETE FROM block WHERE user_id = '$UsErId'";
$result = mysql_query($sql) or die(mysql_error());

if (mysql_query($sql)) 

{
  $redirect = $SITEURL."logout.php";

  header("location:$redirect");

} 

else

{
echo "Delete failed";

}	

}
else

{

echo "You chose not to delete the record";	

}
}
?>

 

Any ideas?

Link to comment
Share on other sites

try this ( you'll have to update with the path and db fileld name )

 

while ($row = mysql_fetch_array($result)) {
   $file = "/path/to/my/files/" . $row['db_field'];
   unlink($file);
   $deletednum++;  THIS IS LINE 51
}

 

Also noticed a $ was missing from the deletednum variable.

Link to comment
Share on other sites

rite got that working, i cant test it untill i select the rite database field, my audios are located in "audio" and in a row call "audio" how do i do this?

 

The current one i have is:

 

$SQL = "SELECT * FROM audio `audio` where user_id = '$UsErId';";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.