'Change all names of files in subdirectories and then move those files one level up

I have the following structure:

news
-1
--240
---img1.jpg
---img2.jpg
-2
--320
---img8.jpg
---img12.jpg
-3
--827
---img4.jpg
---img7.jpg

I am trying to change the filename and add a dash with an id from a database that has the same filenames saved and them move that file one level up the directory.

So for example img1.jpg has an id of 24, the name needs to be: img1-24.jpg and after that be moved one level up.

I have an array that looks like this (from my database):

Array
(
    [0] => Array
        (
            [id] => 14
            [mediable_type] => Topsite\News\Models\News
            [mediable_id] => 1
            [type] => image
            [name] => nieuws-3.jpg
            [mime] => image/jpeg
            [size] => 78733
            [sort] => 1
            [title] => nieuws-3.jpg
            [description] => 
            [original] => 
            [created_at] => 2022-04-25 13:28:27
            [updated_at] => 2022-04-25 13:28:27
            [driver] => s3
        )

    [1] => Array
        (
            [id] => 15
            [mediable_type] => Topsite\News\Models\News
            [mediable_id] => 2
            [type] => image
            [name] => nieuws-1.jpg
            [mime] => image/jpeg
            [size] => 61401
            [sort] => 1
            [title] => nieuws-1.jpg
            [description] => 
            [original] => 
            [created_at] => 2022-04-25 13:28:54
            [updated_at] => 2022-04-25 13:28:54
            [driver] => s3
        )

    [2] => Array
        (
            [id] => 16
            [mediable_type] => Topsite\News\Models\News
            [mediable_id] => 3
            [type] => image
            [name] => nieuws-2.jpg
            [mime] => image/jpeg
            [size] => 105483
            [sort] => 1
            [title] => nieuws-2.jpg
            [description] => 
            [original] => 
            [created_at] => 2022-04-25 13:29:14
            [updated_at] => 2022-04-25 13:29:14
            [driver] => s3
        )

The file structure is related to this array.

For in above example 1 is the [id], 240 is the [mediable_id] and the image name is [name].

I am trying to remove the mediable_id from the file structure all together so the structure will just be the id and then the files in it but with the id in the filename.

I made the following two scripts that I need to combine.

First script that moves the files one level up:

// Change media structure
$news_dir = public_path('news');

$directories = glob($news_dir . '/*' , GLOB_ONLYDIR);
foreach($directories as $dir){
    $directories_lvl2 = glob($dir . '/*' , GLOB_ONLYDIR);
    foreach($directories_lvl2 as $dir2){
        $files = scandir($dir2);
        $files = array_diff(scandir($dir2), array('.', '..'));
        //rmdir($dir2);
        foreach($files as $file){
            $filename = basename($file);
            $oldpath = $dir2.'/'.$filename;
            $newpath = $dir.'/'.$filename;

            rename($oldpath, $newpath);

          echo $oldpath.'<br>';
          echo $newpath.'<br><br>';
        }
    }
}

And this script which gets all files from my database (above example array), loops them and changes the filename to what I need.

// Loop results from media db and change file name
foreach($media as $key1 => $media_array){
    foreach($media_array as $key2 => $media_item){
        if($key2 == 'name'){
            $ext = pathinfo($media[$key1]['name'], PATHINFO_EXTENSION);
            $filename_id = pathinfo($media[$key1]['name'], PATHINFO_FILENAME);
            if($media[$key1]['name'] == $filename_id.'.'.$ext){
                $newfilename = $filename_id.'-'.$media[$key1]['id'].'.'.$ext.'<br>';

                echo $newfilename;
            }
        }
    }
}

Now I need to combine these scripts so that first all names are changed and then the files are moved a level up.

How can I do this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source