steviez Posted February 9, 2007 Share Posted February 9, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/ Share on other sites More sharing options...
ikmyer Posted February 9, 2007 Share Posted February 9, 2007 in the delete process run a query on the users files then run thought them all deleting them... http://us3.php.net/manual/en/function.unlink.php Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180684 Share on other sites More sharing options...
steviez Posted February 9, 2007 Author Share Posted February 9, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180698 Share on other sites More sharing options...
ikmyer Posted February 9, 2007 Share Posted February 9, 2007 what information do you store (if any?) about the files each user has uploaded? Do you store the paths by chance ? Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180804 Share on other sites More sharing options...
sspoke Posted February 9, 2007 Share Posted February 9, 2007 question is how do you know those files are binded to that user who is deleting his account if you store all files in mysql its not smart to delete his sql queries first because then you won't know what files you have to delete. Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180808 Share on other sites More sharing options...
steviez Posted February 9, 2007 Author Share Posted February 9, 2007 all files are stored in the database, how would i delete files first then the sql data? Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180816 Share on other sites More sharing options...
sspoke Posted February 9, 2007 Share Posted February 9, 2007 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.. Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180818 Share on other sites More sharing options...
steviez Posted February 9, 2007 Author Share Posted February 9, 2007 its not working for me Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180853 Share on other sites More sharing options...
ikmyer Posted February 9, 2007 Share Posted February 9, 2007 do you get any errors when you run the code from sspoke Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180862 Share on other sites More sharing options...
sspoke Posted February 9, 2007 Share Posted February 9, 2007 thats a pretend sql query I dont know how your structure looks no way I can really but its just a example Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180866 Share on other sites More sharing options...
steviez Posted February 9, 2007 Author Share Posted February 9, 2007 Yes, but i cant remember what they were, unexpected T_somthing Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180867 Share on other sites More sharing options...
sspoke Posted February 9, 2007 Share Posted February 9, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180868 Share on other sites More sharing options...
steviez Posted February 9, 2007 Author Share Posted February 9, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180880 Share on other sites More sharing options...
ikmyer Posted February 9, 2007 Share Posted February 9, 2007 is /audio/ the field name in the database ? try unlink($row['xxxx']); where xxxx is the name of the field that has the path to the file. ( assuming its the full path and file name ) Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180891 Share on other sites More sharing options...
steviez Posted February 9, 2007 Author Share Posted February 9, 2007 the database does not have the full path, just the name of the file This is line 51 and the offending error: deletednum++; Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180895 Share on other sites More sharing options...
sspoke Posted February 9, 2007 Share Posted February 9, 2007 o ya deletednum++; THIS IS LINE 51 has to have a dollarsign rofl. ya unlink("C:\whatever\".$row["audio"]); if you have no fullpath Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180897 Share on other sites More sharing options...
ikmyer Posted February 9, 2007 Share Posted February 9, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180898 Share on other sites More sharing options...
sspoke Posted February 9, 2007 Share Posted February 9, 2007 sorry I have a problem I forgot $'s sometimes or even ()'s when I do stuff to quick ya what ikmyer said should do it unless your audio files don't have .mp3 at the end you would have to add a .".mp3" after the row Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180903 Share on other sites More sharing options...
ikmyer Posted February 9, 2007 Share Posted February 9, 2007 no need to explain sspoke i have been a victim of this many times as I assume everyone else on the board has ( some less then others ) Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180906 Share on other sites More sharing options...
steviez Posted February 9, 2007 Author Share Posted February 9, 2007 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';"; Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180913 Share on other sites More sharing options...
sspoke Posted February 9, 2007 Share Posted February 9, 2007 you had it right in your deleting part SQL = "SELECT * FROM audio `audio` where user_id = '$UsErId';"; to SQL = "SELECT * FROM `audio` where user_id = '$UsErId';"; too much audios now hehe well cya going to watch anime Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180920 Share on other sites More sharing options...
steviez Posted February 9, 2007 Author Share Posted February 9, 2007 Still no joy, i am getting no errors what so ever but the file is still not deleting from the server Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-180954 Share on other sites More sharing options...
ikmyer Posted February 9, 2007 Share Posted February 9, 2007 can you repost your current code. Quote Link to comment https://forums.phpfreaks.com/topic/37771-solved-file-delete-help/#findComment-181017 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.