For my wife's birthday party, I set up a website through which I collected images of party guests and then created a nice book as a keepsake. Guests have an account and upload images to "their" folder. I now have a tool that dynamically creates slideshows from pictures - but unfortunately it cannot traverse subfolders. So my main goal is to copy the image file to a specified folder from which the slideshow can take it. I would then run a PHP script as cron every 5 minutes or so and display the image on the screen for the duration of the party.
I've found a bunch of code snippets that all do the same thing: They copy all files and folders recursively to the defined destination. For example. 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, but the truth is it's not what I need. The script is copying files and folders and placing the files into the same subfolder they are in. My problem is that I don't want to create subfolders. I just want the script to go through all subfolders and copy the found image files into a folder. I thought this should be an easy thing for newbies, but it seems I was wrong.
Can anyone help me achieve this? Thanks!
P粉0837850142024-01-30 00:50:02
IT Goldman was right - leave $to out of the equation; I even removed the $to variable entirely and put my path in the copy parameter:
0) { foreach ($all as $a) { $ff = basename($a); // CURRENT FILE/FOLDER if (is_dir($a)) { copyfolder("$from$ff/"); } else { if (!copy($a, "C:/TARGET/$ff")) { exit("Error copying $a to C:/TARGET/$ff"); } echo "$a copied to C:/TARGET/$ff\r\n"; } }} } // (B) GO! copyfolder("C:/SOURCE/"); ?>