Jump to content

Deleting 3000 files!


alej469

Recommended Posts

 

Hey everyone, been trying to solve this problem myself for a while now, but to no avail...

 

I've got a directory full of about 11,000 mp3 files. Most files are in a database which I started using a couple of years ago, the rest are redundant. I'm looking for a way to delete any files which are not listed in the database.

 

I've made some progress with the coding for this, but as you can imagine I'm on edge because any mistakes could be catastrophic! What the code below basically does is put all files in the directory in one array, and all the files in the database in another array. I've then used array_diff to calculate the difference and show the files which are not in the database.

 

What I'm now trying to do is delete all the files in the array ($result). Can anyone help me finish the code for this?

 

Cheers in advance  :)

Link to comment
Share on other sites

Lol, missed out the code:

 

<?php 
//get path of directory containing this script
$dir = $_SERVER['DOCUMENT_ROOT'].dirname($PHP_SELF);
//open a handle to the directory
$handle = opendir($dir);
//intitialize our counter
$count = 0;
$alldirfiles = array("TEST", "TEST2");
$alldbfiles = array("TEST", "TEST2");
//loop through the directory
while (false !== ($file = readdir($handle))) {
//evaluate each entry, removing the . & .. entries
if (is_file($file) && $file !== '.' && $file !== '..') {
$count++;
array_push($alldirfiles, $file);
}
}


/////////////////////////////////////////////////////////

mysql_connect("host","name","pass");
@mysql_select_db("thisdb") or die( "Unable to select database");
$query222="SELECT * FROM table ORDER BY id ASC";
$result222=mysql_query($query222);
$num222=mysql_numrows($result222);
mysql_close();

$i=0;
while ($i < $num222) {
$rfilename = mysql_result($result222,$i,"filename");
array_push($alldbfiles, $rfilename);
$i++;
}


$result = array_diff($alldirfiles, $alldbfiles);
$resultminus = count($result);
echo "Total files: ".$count."<br>";
echo "Database files: ".$num222."<br>";
echo "To be deleted files: ".$resultminus."<br>";

print_r($result);
?>

Link to comment
Share on other sites

One thing I assume you've done already is used print_r of your various arrays to make sure they contain the values you want them to.  It will definitely print A LOT of stuff to your browser, but you only have to check a few items.

 

Another option is to randomly select 20 or so files from the $result array and check to see if they are in the $num222 array.  If they are not in the database, then chances are extremely good that you have set up your stuff correctly (let's suppose you want to delete 50% of the files then the probability that you have randomly chosen 20 files that are just randomly outside of your dataset is 1/2^20 which is a VERY SMALL number).

 

Once you've established that you have the right files, you can use the file functions at www.php.net to delete the files.

 

My other suggestion is, create a new database and populate it with 10 file names, and put 20 files in a new directory, and repeat the process you've done already with that much smaller subset until you perfect the appropriate deletion of exactly the files you want deleted.  This way you can make mistakes with many fewer files, that are essentially expendable.

Link to comment
Share on other sites

 

I've used print_r and checked that the files listed in the new array are not listed in the database, so the new array is displaying the files that I need to delete, I just need to know how to delete files in an array, as I'm not 100% confident.

 

The array outputs:

Array ( [2] => fileone.mp3 [7] => filetwo.mp3 [8] => filethree.mp3 ) + 2997 others!

 

How would i use unlink to delete these files?

Link to comment
Share on other sites

First test your script in a different directory to make sure it works as expected.  I'd put 20 throw-away files in a different directory and run a version of this script below, where you specify the files you want in an array called $result.

 

ie.

 

$result = array("filename1.txt", "filename2.txt", "filename3.txt");

 

and confirm that those are the only files that are deleted using the script below.

Then use something like this to delete the extra files and adding this to the script you already have.

 

foreach ($result as $key => $filename) {
  if (file_exists($filename)) {
    unlink($filename);
  }
}

 

 

 

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.