chrisgrim's avatar

Simple File Saving From Local To Server

Hi,

I am trying to understand the laravel file saving structure. My overall issue is moving from local to my server. I created a very simple project to test file saving and understand it better. I have already done the php artisan storage:link. In my web.php file I have

Route::post('process', function (Request $request) {
    // cache the file
    $file = $request->file('photo');

    // generate a new filename. getClientOriginalExtension() for the file extension
    $filename = 'profile-photo-' . '.' . $file->getClientOriginalExtension();

    // save to storage/app/photos as the new $filename
    $path = $file->storeAs('public', $filename);

});

When I am on local I can call this file by using

<img src="/profile-photo-.jpg">

My issue is when I upload my project to the server I now have to use

<img src="/storage/profile-photo-.jpg">

I must be doing something wrong because it can't be default that I would have to change all my link urls whenever I move between local and server.

Thanks!

0 likes
6 replies
Snapey's avatar

Looks like local is wrong. You should be getting the file from the storage folder.

Are your filesystem settings the same between systems?

Where is the file actually stored?

chrisgrim's avatar

@Snapey My method of moving to the server is pretty simple so I think the settings should be the same. I created a new laravel project on my server and a new project on local. Then uploaded from my local, which overwrote everything on the server except for the vendors folder and any hidden files. All I changed was the web.php file and the page on my local.

The file is saved in storage/app/public/filename.jpg or public/storage/filename.jpg

Snapey's avatar
Snapey
Best Answer
Level 122

So if your document root is public, then the img src should be "/storage/filename.jpg"

Check you don't have two copies of the file on your local machine and you are displaying the wrong one

chrisgrim's avatar

@Snapey

That worked! Oh man, that was such a simple fix. I only have one file in the directory so I am not sure why it works for both /storage and no /storage. I will just assume I have to have the storage there from now on.

Thanks again!

Please or to participate in this conversation.