0

need a bit of help, I am trying to create a PHP script that will be initiated by a button from the admin page, which will find all image names in the database under "photos" and then delete any images from my directory 'userimages' that are no longer in use in the database?

I honestly have no idea where to start with this because its kind of confused me a bit? Is there a simple way to go about this?

AidanPT
  • 185
  • 1
  • 3
  • 12
  • 1
    Break the problem down into it's pieces, list out each task that you need to accomplish in the order they need to be performed. Work on implementing each task individually then assess the whole algorithm when you're completed to look for ways to potentially increase performance if necessary. If you have issues implementing a portion - come back and ask us demonstrating what you did, what you expected to get from it and what it actually gave you (errors included). – Brandon Buck Mar 28 '14 at 17:20
  • I would do that but I actually don't understand how to write a code for this? Because I don't know how to search for file names in the directories of my site? – AidanPT Mar 28 '14 at 17:22
  • The PHP Manual is a great resource for finding out how to do things in PHP, specifically [here](http://www.php.net/manual/en/function.scandir.php) where they talk about `scandir` and listing files in a directory. – Brandon Buck Mar 28 '14 at 17:23

1 Answers1

1

You should get all image names as array
than read all images name from the folder

if ($handle = opendir('/path/to/files')) {
    echo "Directory handle: $handle\n";
    echo "Entries:\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($entry = readdir($handle))) {
        echo "$entry\n";
    }

    /* This is the WRONG way to loop over the directory. */
    while ($entry = readdir($handle)) {
        echo "$entry\n";
    }

    closedir($handle);
}


compare and remove difference

$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
Anri
  • 1,706
  • 2
  • 17
  • 36