move files to folder script not working as planned

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]

is not working

Define: is not working? What result are you getting and what result do you expect and exactly at what point in the code isn’t it working?

BTW - you need to do something with the value being returned by the function call. You need to either assign it to a variable and use that variable in the code or use the function call at the point in the code where you want to use the value it returns.

Sorry for the late reply, i have abandoned this project and went another route with it. Karma given, and apologies for not writing sooner.

Sponsor our Newsletter | Privacy Policy | Terms of Service