foreach - Copy Files from folders to folder tree in php -


i have large number of books need categorize language , libraryid.

the file-names structured , spread out in many folders:

eng_filename_libraryname1.pdf eng_filename_libraryname2.pdf spa_filename_libraryname1.pdf spa_filename_libraryname2.pdf 

i need move them folders this

eng ->libraryname1 --eng_filename_libraryname1.pdf ->libraryname2 --eng_filename_libraryname2.pdf 

here's code:

foreach (glob("c:/wamp/www/projects/filemove/eth/*") $folderpath) { $foldername = preg_replace('/.*?\/(.*?)/', '$1', $folderpath);     foreach (glob("c:/wamp/www/projects/filemove/eth/*/*") $librarypath) {     $libraryname = preg_replace('/.*?\/(.*?)/', '$1', $librarypath);          foreach (glob("c:/wamp/www/projects/filemove/pdf/*.pdf") $filepath) {            $ethologue = preg_replace('/(.*?)_.*/', '$1', $filepath);            $library = preg_replace('/.*?_.*?_.*?_(.*?)_.*/', '$1', $filepath);            $filename = preg_replace('/.*?\/(.*?)/', '$1', $filepath);             if ($ethologue = $foldername ) {            if ($library = $libraryname) {                 copy($filepath, $librarypath);                 }            }         }     } } 

thanks in advance!

you can give try

define("file_copy", 1); define("file_move", 2);  $dirsource = __dir__ . "/case"; $dirdestination = $dirsource . "/books"; $cache = $errors = array(); $allowduplicate = false; $mode = file_copy; // copy or move file ;  try {     $dirit = new filesystemiterator($dirsource, filesystemiterator::skip_dots);     $regexit = new regexiterator($dirit, '/.pdf$/i');      // make directory     if (! is_dir($dirdestination) , ! @mkdir($dirdestination, 0777, true)) {         throw new exception("destination folder ($dirdestination) not exist");     }      foreach($regexit $splfile) {         $hash = md5_file($splfile);         $ext = "." . $splfile->getextension();          // don't take duplicates         if (! $allowduplicate && isset($cache[$hash])) {             continue;         }         // split file name         list($category, $name, $subcategory) = explode("_", $splfile->getbasename($ext));         if (empty($category) || empty($name) || empty($subcategory)) {             $errors[] = "file ($splfile) not have valid name format";         }          // new file path         $path = sprintf("%s/%s/%s", $dirdestination, $category, $subcategory);         if (! is_dir($path) , ! @mkdir($dirdestination, 0777, true)) {             throw new exception("destination folder ($path) not exist");         }          $filetmp = $path . directory_separator . $splfile->getfilename();          if (is_file($filetmp)) {              if (! $allowduplicate) {                 // check if file duplicate                 $copyhash = md5_file($filetmp);                 if ($hash == $copyhash) {                     continue;                 }             }             $x = 1;             while(is_file($filetmp)) {                 $filetmp = $path . directory_separator . $splfile->getbasename($ext) . "_" . $x . $ext;                 $x ++;             }         }          if ($mode == file_copy) {             if (! copy($splfile, $filetmp))                 $errors[] = "file ($splfile) failed copy";         } else {             if (! rename($splfile, $filetmp))                 $errors[] = "file ($splfile) failed move";         }         $cache[$hash] = true;     } } catch (exception $e) {     echo $e->getmessage(); } 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -