For the birthday party of my wife I have setup a website through which I am collecting images from the party guests to create a nice book afterwards as a souvenir. The guests have an account and do upload the images to 'their' folders. I now have a tool in place that is dynamically creating a slide show from the pictures - but unfortunately it cannot go through subfolders. So my primary goal is to copy the image files to one specified folder, from where the slideshow can pick it up. I would then run the PHP script as a cron every 5 minutes or so, and show the images on a screen during the party.
I already found a bunch of code snippets that all do the same thing: They copy recursively all files and folders to a defined destination. E.g. this one (taken from here: https://code-boxx.com/copy-folder-php/):
<?php
// (A) COPY ENTIRE FOLDER
function copyfolder ($from, $to, $ext="*") {
// (A1) SOURCE FOLDER CHECK
if (!is_dir($from)) { exit("$from does not exist"); }
// (A2) CREATE DESTINATION FOLDER
if (!is_dir($to)) {
if (!mkdir($to)) { exit("Failed to create $to"); };
echo "$to created\r\n";
}
// (A3) GET ALL FILES + FOLDERS IN SOURCE
$all = glob("$from$ext", GLOB_MARK);
print_r($all);
// (A4) COPY FILES + RECURSIVE INTERNAL FOLDERS
if (count($all)>0) { foreach ($all as $a) {
$ff = basename($a); // CURRENT FILE/FOLDER
if (is_dir($a)) {
copyfolder("$from$ff/", "$to$ff/");
} else {
if (!copy($a, "$to$ff")) { exit("Error copying $a to $to$ff"); }
echo "$a copied to $to$ff\r\n";
}
}}
}
// (B) GO!
copyfolder("C:/SOURCE/", "C:/TARGET/");
?>
This works fine besides the fact, that it is not what I need. The script is copying files and folders and is placing the files into the same subfolders where they are coming from. My issue is that I do not want the subfolders to be created. I just want the script to go through all the subfolders and copy the image files found into one single folder. I thought this should be an easy thing for a newbie, but seems as if I am wrong here.
Can anyone help me to achieve that? Thanks!