Hey all, i have this script that i was wanting to take all the .png files from the /svss folder and move them into a folder structure based on thier creation date. it kinda works as long as there are no duplicate names, so i added a function to rename the duplicates with _X and that is not working can i get some fresh eyes on this.
[php]
<?php error_reporting(E_ALL); //setup some vars /root path of .png files $mpath = __DIR__ .'/svss'; $count = 0; $errors = 0; $ok = 0; function file_newname($path, $filename){ if ($pos = strrpos($filename, '.')) { $name = substr($filename, 0, $pos); $ext = substr($filename, $pos); } else { $name = $filename; $ext = ""; } $newpath = $path.'/'.$filename; $newname = $filename; $counter = 0; while (file_exists($newpath)) { $newname = $name .'_'. $counter . $ext; $newpath = $path.'/'.$newname; $counter++; } return $newname; } //Delete all .htm files array_map('unlink', glob( "$mpath/*.htm")); //Loop through all .png files foreach (glob("$mpath/*.png") as $file) { //set variables $lastmoddate = filemtime($file); $basename = basename($file); $month = date("M", $lastmoddate); $day = date("d", $lastmoddate); $year = date("Y", $lastmoddate); $newPath = __DIR__ .'/svss/' .$year. '/' .$month. '/' .$day. '/'; $count++; //See if the new directory is already created, then create if not if (!is_dir($newPath)) { mkdir($newPath, 0775, true); } //put the new path and the file together $newName = '/' .$year. '/' .$month. '/' .$day. '/' .$basename; $old_Path = $mpath. '/' .$basename; $new_Path = $mpath.$newName; // Move the file if it doesn't exist and count each iteration if (file_exists($newPath.$basename)) { $errors++; file_newname($mpath, $basename); rename($old_Path, $new_Path); continue; } else { $ok++; rename($old_Path, $new_Path); continue; } } echo 'Total: ' .$count. ''; echo 'Errors: ' .$errors. '
'; echo 'Moves: ' .$ok. '
'; ?>
[/php]