-2

I'm struggling to find any information on looping through all subfolders within a folder and renaming them using PHP. I am renaming them so that I do not reach any limits on numbers of subfolders.

At the moment my user folder structure is as follows :

images/logos/1216/logo.jpg
images/logos/11437/logo.jpg
images/logos/234438/logo.jpg

So I want to loop through all folders within the 'logos' and rename them as follows :

images/users/1/1216/logos/logo.jpg
images/users/11/11437/logos/logo.jpg
images/users/234/234438/logos/logo.jpg

To calculate the new subfolder name, i'm going to take the existing user id (i.e. 11437 and divide by 1000).

The actual issue is how, do I loop through all the subfolders and what is the best way to rename the folder structure.

Mark
  • 79
  • 1
  • 7
  • This isn't really a question of renaming since you are making a completly different structure. I'd say you should copy the files and then remove the original. – Andreas Jul 16 '17 at 13:07
  • Is all files called logo.jpg? – Andreas Jul 16 '17 at 13:12
  • yes, all the files are called logo.jpg at the moment – Mark Jul 16 '17 at 13:25
  • yes I understand that its a totally different structure, I was just wondering if the structure could be renamed, or as you say, would it be best to loop through all of the subfolders, and for each subfolder, copy the file to the new directory structure, then delete all existing subfolders? – Mark Jul 16 '17 at 13:27
  • Look at my answer below. Please note I just edited it with two lines of code `$error` – Andreas Jul 16 '17 at 13:32

1 Answers1

0

If I'm correct about the first line of code here you sholud get an array lookin like the second line. Test that first and make a backup of your files.

This should list all userid's or directories and loop through them.
If the copy is success (true) it unlinks the old file, else returns a error message.

This is untested code

//$arr = array_filter(glob('images/logos/*'), 'is_dir');
$arr = array("images/logos/1216","images/logos/11437","images/logos/234438");
//var_dump($arr);

foreach($arr as $dir){
    $UID = str_replace("images/logos/", "", $dir);
    $newpath = "images/users/" . floor($UID/1000) . "/" . $UID . "/logos/logo.jpg";
    $oldpath = $dir . "/logo.jpg";
    if(copy($oldpath, $newpath)){
        unlink($oldpath);
    }else{
        echo "error with file " . $oldpath . "<br>\n";
        $error = true;
    }
}

if(!$error) unlink("images/logos");

EDIT; Just noticed I had used Excel too much lately. Changed the & to .
EDIT2; tested the code on my own page now and it seems the glob returns the path. Included code to get the UserID.

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • Thanks for the code above, the issue I have is that there are thousands of subfolders in the logos folder, I wanted a method to loop through all subfolders within the logos folder and not to provide an array. – Mark Jul 16 '17 at 14:04
  • @Mark yes... The foreach does loop through them. – Andreas Jul 16 '17 at 14:13
  • Thanks I see the commented out code to get all subdirectories now, thanks – Mark Jul 16 '17 at 14:18
  • @Mark does it work? I see that you have 10 questions asked and none of them has an accepted answer and you have not even upvoted any of them. Maybe it's time now after almost a year and a half to take the tour and understand how SO works and get the informed badge? https://stackoverflow.com/tour – Andreas Jul 16 '17 at 14:47
  • If you need more SO guidance read here https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Andreas Jul 16 '17 at 14:49
  • Thanks, I havent tried the code yet, but will do today and read the guidance on here as you have mentioned, thanks. – Mark Jul 17 '17 at 07:06
  • 1
    Hi Andreas, the code worked, thanks for your help. The only thing I had to add was a call to mkdir before the copy statement, as the new folders didn't exist and therefore couldn't copy the files to the new folder. – Mark Jul 17 '17 at 13:11