Predrag Beocanin
Predrag Beocanin

Reputation: 1400

Delete files not matching a list

So I'm trying to make a simple script, it will have a list of predefined files, search for anything that's not on the list and delete it.

I have this for now

<?php
$directory = "/home/user/public_html";
$files = glob($directory . "*.*");

foreach($files as $file)
{

    $sql = mysql_query("SELECT id FROM files WHERE FileName='$file'");
    if(mysql_num_rows($sql) == 0)
        unlink($directory . $file);
}
?>

However, I'd like to avoid the query so I can run the script more often (there's about 60-70 files, and I want to run this every 20 seconds or so?) so how would I embedd a file list into the php file and check against that instead of database?

Thanks!

Upvotes: 0

Views: 729

Answers (2)

Chris
Chris

Reputation: 668

You are missing a trailing / twice.. In glob() you are giving /home/user/public_html*.* as the argument, I think you mean /home/user/public_html/*.*.

This is why I bet nothing matches the files in your table.. This won't give an error either because the syntax is fine.

Then where you unlink() you do this again.. your argument home/user/public_htmltestfile.html should be home/user/public_html/testfile.html.

I like this syntax style: "{$directory}/{$file}" because it's short and more readable. If the / is missing, you see it immediately. You can also change it to $directory . "/" . $file, it you prefer it. The same goes for one line conditional statements.. So here it comes..

<?php
$directory = "/home/user/public_html";
$files = glob("{$directory}/*.*");

foreach($files as $file)
{

    $sql = mysql_query("SELECT id FROM files WHERE FileName=\"{$file}\";");
    if(mysql_num_rows($sql) == 0)
    {
        unlink("{$directory}/{$file}");
    }
}
?>

EDIT: You requested recursion. Here it goes..

You need to make a function that you can run once with a path as it's argument. Then you can run that function from inside that function on subdirectories. Like this:

<?php

/*
    ListDir list files under directories recursively
    Arguments: 
    $dir        = directory to be scanned
    $recursive  = in how many levels of recursion do you want to search? (0 for none), default: -1 (for "unlimited")

*/

function ListDir($dir, $recursive=-1)
{
    // if recursive == -1 do "unlimited" but that's no good on a live server! so let's say 999 is enough..
    $recursive = ($recursive == -1 ? 999 : $recursive);

    // array to hold return value
    $retval = array();

    // remove trailing / if it is there and then add it, to make sure there is always just 1 / 
    $dir = rtrim($dir,"/") . "/*";

    // read the directory contents and process each node
    foreach(glob($dir) as $node) 
    {
        // skip hidden files
        if(substr($node,-1) == ".") continue;

        // if $node is a dir and recursive is greater than 0 (meaning not at the last level or disabled)
        if(is_dir($node) && $recursive > 0) 
        {
            // substract 1 of recursive for ever recursion.
            $recursive--;
            // run this same function again on itself, merging the return values with the return array
            $retval = array_merge($retval, ListDir($node, $recursive));
        } 
        // if $node is a file, we add it to the array that will be returned from this function
        elseif(is_file($node)) 
        {
          $retval[] = $node;

          // NOTE: if you want you can do some action here in your case you can unlink($node) if it matches your requirements..

        }
    }
    return $retval;
}

// Output the result
echo "<pre>";
print_r(ListDir("/path/to/dir/",1));
echo "</pre>";

?>

Upvotes: 2

user2625787
user2625787

Reputation:

If the list is not dynamic, store it in an array:

$myFiles = array (
    'some.ext',
    'next.ext',
    'more.ext'
);
$directory = "/home/user/public_html/";
$files = glob($directory . "*.*");

foreach($files as $file)
{
    if (!in_array($file, $myFiles)) {
       unlink($directory . $file);
    }
}

Upvotes: 1

Related Questions