0

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!

  • Keep your `$to` folder out of the loop, maybe even a constant. So all the files would be copied there for sure. – IT goldman Jun 22 '22 at 21:14
  • Does this answer your question? [Is There A Way To glob() Only Files?](https://stackoverflow.com/questions/14084378/is-there-a-way-to-glob-only-files) – imvain2 Jun 22 '22 at 21:21
  • The accepted answer for the above question has a solution: https://stackoverflow.com/a/14084379/3684265 – imvain2 Jun 22 '22 at 21:22

1 Answers1

0

IT goldman was right - keep the $to out of the loop; I have even removed the $to variable completely and placed my path in the copy-argument:

<?php
// (A) COPY ENTIRE FOLDER
function copyfolder ($from, $ext="*") {
  // (A1) SOURCE FOLDER CHECK
  if (!is_dir($from)) { exit("$from does not exist"); }
        
  // (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/");
    } 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/");
?>