Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

warmwhisky's avatar

Rename file when using Docker

Laravel 9 with Docker

When I check if a file exists it return true and then when I attempt to rename it it does not rename it.

When I dd out the new filename and location I get

var/www/html/public/images/products/shop/1000/03-BBB.jpg

But on the hard drive its location is

/home/ng/Projects/online-shop/public/images/products/shop/1000/03-BBB.jpg

so it never actually renames it.

Here is my rename if statement

if (file_exists(public_path() . '/images/products/shop/1000/03-B.jpg')) {
    Storage::move(public_path() . '/images/products/shop/1000/03-B.jpg', public_path() . '/images/products/shop/1000/03-B.jpg');
}
0 likes
3 replies
aleahy's avatar

Both of those file locations are actually the same place. Docker has mapped your home/ng/Projects/online-shop folder to its own /var/www/html folder. It's basically a virtual linux installation and your folder is injected into a location in the docker's file system. So running dd will give you a response from Docker's perspective.

Your rename statement doesn't actually rename anything. The old path is exactly the same as the new path, so nothing should change.

warmwhisky's avatar

@aleahy Sorry I had misspelled it. The renamed version is actually '/images/products/shop/1000/03-BBB.jpg'

The full code which is less readable is as follows

$paths = [
    "/images/products/shop/1000/",
    "/images/products/shop/300/",
    "/images/products/shop/50/",
];

$file_ends = [
    ".jpg",
    "_hover.jpg",
    "_1.jpg",
    "_2.jpg",
    "_3.jpg",
    "_4.jpg",
    "_5.jpg",
    "_6.jpg",
    "_7.jpg",
    "_8.jpg",
    "_9.jpg",
    "_10.jpg",
];

foreach ($paths as $path) {
    foreach ($file_ends as $file_end) {
        if (file_exists(public_path() . $path . $old_code . $file_end)) {
            
            Storage::move(
                public_path() . $path . $old_code . $file_end,
                public_path() . $path . $new_code . $file_end
            );
            
            dd(public_path() . $path . $new_code . $file_end);
        }
    }
}
warmwhisky's avatar
warmwhisky
OP
Best Answer
Level 1

I was Storage::move but when I changed it to File::move it worked.

foreach ($paths as $path) {
    foreach ($file_ends as $file_end) {
        if (file_exists(public_path() . $path . $old_code . $file_end)) {
            File::move(
                public_path() . $path . $old_code . $file_end,
                public_path() . $path . $new_code . $file_end
            );
        }
    }
}

Please or to participate in this conversation.