Current location: Hot Scripts Forums » Programming Languages » PHP » Delete files/directories selected in MySQL


Delete files/directories selected in MySQL

Reply
  #1 (permalink)  
Old 08-04-05, 03:00 PM
ronlar ronlar is offline
Newbie Coder
 
Join Date: Jun 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Question Delete files/directories selected in MySQL

I have a PHP page which creates a temporary directory, copies 1 file to that directory and inserts an entry in MySQL.

I want another PHP page to select all the entries in MySQL which are inserted more than 10 minutes ago, delete the files/directories and then delete the entries in MySQL.

The following code works, but it deletes only one file/directory at a time and also deletes the entry in MySQL. How can I make it delete all the selected files/directories at the same time and delete all the entries in MySQL?

Code:
<?php

include("settings.php");

$link = @mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
$database = mysql_select_db(DB_NAME);
$sql = "SELECT * FROM temp WHERE DATE_ADD(time, INTERVAL 10 MINUTE) < NOW()";
$result = mysql_query("$sql");
while ($out = mysql_fetch_array($result)) {
   $dirname = $out['directory'];
   $filename = $out['filename'];
}
mysql_close($link);

$file = "$dirname/$filename";

if(file_exists($file))
unlink($file);

rmdir($dirname);

$link = @mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
$database = mysql_select_db(DB_NAME);
$sql = "DELETE FROM temp WHERE directory = '$dirname'";
mysql_query("$sql");
mysql_close($link); 

?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 08-04-05, 03:06 PM
FiRe FiRe is offline
Code Guru
 
Join Date: Oct 2004
Location: UK
Posts: 801
Thanks: 0
Thanked 0 Times in 0 Posts
put the code in the loop
__________________
Alexa Share <-- Trade virtual shares in websites with this online game.

codR.us <-- Submit and vote for your favorite code snippets with codR.us.

XEWeb.net <-- The ultimate PHP resource network.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 08-06-05, 09:58 AM
ronlar ronlar is offline
Newbie Coder
 
Join Date: Jun 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
What kind of loop should I use and how do I insert the loop?

I have tried different ways, but none of them works, I only receive error messages.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 08-08-05, 09:41 AM
ronlar ronlar is offline
Newbie Coder
 
Join Date: Jun 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Smile

The matter is solved. By placing the code inside the while loop it works perfect.
Code:
<?php

include("settings.php");

$link = @mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
$database = mysql_select_db(DB_NAME);
$sql = "SELECT * FROM temp WHERE DATE_ADD(time, INTERVAL 10 MINUTE) < NOW()";
$result = mysql_query("$sql");
while ($out = mysql_fetch_array($result)) { 
   $dirname = $out['directory'];
   $filename = $out['filename'];

   $file = "$dirname/$filename";

   if(file_exists($file))
   unlink($file);

   rmdir($dirname);
   
   $link = @mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
   $database = mysql_select_db(DB_NAME);
   $sql = "DELETE FROM temp WHERE directory = '$dirname'";
   mysql_query("$sql");
}

mysql_close($link);

?>

Last edited by ronlar; 08-08-05 at 09:52 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 03:22 AM
delete duplicate rows in mysql using php seans9 PHP 4 07-16-08 10:42 PM
auto delete records in Mysql Bazzio PHP 3 07-18-05 09:46 PM
Delete row from mysql error. bigkid PHP 1 08-05-04 10:36 PM
select & delete mysql queries superman PHP 2 07-20-03 04:08 AM


All times are GMT -5. The time now is 12:40 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.