mateoo88's avatar

File management in the Laravel application

Hi, In my Laravel application I want to implement for the first time file uploading and displaying for users.

To better explain, here's an example: A user submits a problem ticket through a form and attaches several files (e.g., two images, one video, and a PDF invoice). The text fields of the form are stored in the database, and the file names are also saved id DB. The actual files are stored on a configured server or cloud storage.

Later, users who have access to the ticket can view it along with all the attached media files.

For security reasons, I was considering generating Temporary URLs for these files. When a user visits the ticket, temporary links (valid for, say, one hour) would be generated and stored in the database or cache with an expiration time. All users accessing the ticket would use the same temporary URLs when needed.

Does this approach make sense? Or do you handle this differently in your applications? I would appreciate any advice.

0 likes
7 replies
mateoo88's avatar

@Sinnbeck Speaking of S3, here’s my second question. I wanted to start a new topic, but I’ll ask here. I did some research, and most recommendations advise against storing files on the same server as the application. The most recommended approach is to store and manage media files using Amazon AWS S3.

However, one thing that worries me is the cost. AWS S3 charges for both storage and usage. I wouldn’t want the expenses to get out of control over time, which is my main concern.

martinbean's avatar

For security reasons, I was considering generating Temporary URLs for these files. When a user visits the ticket, temporary links (valid for, say, one hour) would be generated and stored in the database or cache with an expiration time. All users accessing the ticket would use the same temporary URLs when needed.

Does this approach make sense? Or do you handle this differently in your applications? I would appreciate any advice.

@mateoo88 No, it doesn’t make sense. You would just generate the URLs as and when the files need to be accessed. You wouldn’t generate URLs, and then store them in the database.

Put a model on your uploaded file model that returns a signed URL:

class UploadedFile extends Model
{
    public function url(): string
    {
        return Storage::temporaryUrl($this->path, now()->addMinutes(5));
    }
}

Then use the method in your views to view the file:

<img src="{{ $uploadedFile->url() }}">
1 like
mateoo88's avatar

@martinbean But in this case, isn’t it that if, for example, three users visit the same ticket, three separate temporary URLs will be generated for the same file?

mateoo88's avatar

@martinbean I mean that I want the solution to be optimal and not generate unnecessary code on every page refresh. Perhaps caching each temporary URL after it’s generated would be a good idea?

martinbean's avatar

@mateoo88 Then use the once helper; temporary URLs will only be generated once per file path in the current request:

public function url(): string
{
    return once(fn () => Storage::temporaryUrl($this->path, now()->addMinutes(5)));
}

But I feel you’re just over-thinking things. Your application isn’t going to blow up generating three temporary URLs. If it does, you seriously need to refactor something.

1 like

Please or to participate in this conversation.