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

dkage's avatar
Level 1

How do I save a url() for a Storage image inside database as a img src?

For context first, the company I work at is changing storage locations. We have some forms with different questions, and the images related to those are either on base64 inside the text on the questions table with the rest of the text, or on AWS cloud.

As the text of each questions uses HTML being written in CKEditor with images in-between, we used to save to database exactly what the HTML CKEditor outputs, using for example inside the text, the img tag where the src would be the URL to the our AWS storage like "aws/image.png"".

As we upgraded recently to a robust local NAS, we now want to abandon AWS and use images hosted locally. The problem is that I'm trying to download each one by getting the URL from inside the HTML saved on the database text column, using a regex for getting the src of every img tag, then using file_get_contents to save locally using a new pattern of directories (different from the current AWS).

    public function download_img($url, $question_id): string
    {
        $public_question_dir = 'images/questions/'.$question_id.'/';
        if (!file_exists(public_path() . '/public/' . $public_question_dir)) {
            mkdir(public_path() . '/public/' . $public_question_dir, 0777, true);
        }

        $filename = Str::random(40) . '.png';
        $fileContents = file_get_contents($url);
        Storage::disk('public')->put('/' . $public_question_dir . $filename, $fileContents);
        
        return $public_question_dir . $filename;
    }

The download works great. It saves to the right public symbolic link folder inside storage/app/public/ as it should, but now I need to do a str_replace on the text column inside the database to replace the old URL to new one locally hosted. The problem is, I have no idea how to do that replacement, because if the URL is resolved using url(), the link will only work for a given server.

If I just replace the the URL using something like

$question = Question::find(129);

# image[0] = old aws link
# image[1] = return of download_img, local path
$question->question_text = str_replace($image[0], url($image[1]), $question->question_text);
$question->save();

the URL is resolved for the current environment, so it will save https://127.0.0.1/, but I need for it the work on every env, production, testing and local. I tried saving to the database something like {{ url(public_link) }} , but then the blade does not resolve it, it prints as a {{}} string as it is already rendering the HTML by using {! !}.

I have no idea how should I do this in a way that works for every environment, or that blade template can interpret and render correctly. Will I have to treat every single URL in the whole HTML text everytime the HTML is being loaded? Is there a way to do this without using plain base64 inside the text of the database?

Thanks in advance for any help and input.

0 likes
7 replies
MohamedTammam's avatar

@Tray2 I think his problem is not how to upload the file, but to change old URLs with new ones that works on all environments.

1 like
dkage's avatar
Level 1

@Tray2 I'm sorry if I wasn't clear enough. That's exactly the problem. How would I set the relative path for the images to load on every environment, for it to work like it does on a blade template, that we call url() or asset() and it creates the image link with the Storage path, and puts the current domain at the beginning of the URL?

Maybe my other reply I just typed to MohammedTammam makes easier for understanding the problem. Anyway, thanks for your time.

MohamedTammam's avatar

If you change all URLs on the production to the new ones, what problem are you going face?

I local development you will use the new way of uploading the files, why would you care about old AWS URLs?

1 like
dkage's avatar
Level 1

@MohamedTammam As we have a rsync that replicates the NAS production project/storage files for another smaller NAS for development, I just wished that I could simply do it in a way that when we dump the production database on a local development environment, we could have the relative paths for every image considering this and load locally.

But I guess you are right, better to just leave as production URL as that having an active internet connection, would load the images aswell with the production URLs. Too bad if happens that we change domains like we did some time ago. I would need to update every column for texts containing the "hard coded" URL inside all the img tags inside the questions texts.

I was looking for a way to make the images load dinamically like when we call the url() or asset() functions for rendering the blades.

Anyway thank for answering.

1 like
MohamedTammam's avatar

@dkage What if you store the relative domain path, like /images/image.png that should work on all environments. It will not be synced of course, but will work for each environment separately. And you will not need to update it later if the domain changes.

Please or to participate in this conversation.