table of products
products_id
products_code
products_name
products_price
images is saved in a folder with product_code value as the folder name
/images/$product_code/1.jpg
/images/$product_code/2.jpg
(for example, if the products_code is A-0, the image will be /images/A-01/1.jpg, /images/A-01/2.jpg, )
When I delete the product from the database. the images folder will become "orphan", no product pointing to this image folder.
I want to write a script. That once a while, run this script.
1) it will get all the folders' names under the folder /images/.
2) it will get all the products_codes from the database.
3) it will match the results of 1) and 2) and get the folders' names from 1) that are not in the products_codes from 2), which means these prducts has been deleted.
4) with the list from 3), i can manually delete these images. or I may have another scripts running to delete these "orphan" image folders.
step 2) is just a simple sql.
step 3) we can use some kind array function. (convert 2) and 3) to array first)
My first question is about step 1)
How can I get all the folders names under the folder /images/?
$path=$_SERVER['document_root']."/images/";
chdir($path);
$dir=opendir(".");
while ($file=readdir($dir))
{
if (($file!=".")&&($file!=".."))
{
$folderName[]=$file;
}
}
Will this script work? folder is also a file right?
My next question, is there any better way to handle these "orphaned" image folders?
the image folders maybe uploaded to ftp and owned by ftp user with permission rw-r--r--. So when I delete the product from database through web access administration interface, the program page cannot delete the image at the same time (apache user cannot delete the file owned by ftp user and permission is rw-r--r--)
Thanks!