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

Christianus's avatar

Store Image to Filesystem

I want to store image and someone come with this statement :

It's better to store the image on the filesystem and only store the image filename in the db. Don't store the image in the db. There is no good reason to do so and it slows things down and takes more memory whereas a simple link to the image on the filesystem removes all of that from the equation. Files->filesystem, Data->database.

Now the problem, how could i do it ? please guide me step by step and how to filter the file (give the protection) so if the input file extention is wrong (ex : not .jpg) the button disable and flash massage appear

Here is code for my blade form

                    <div align="center">
                        <h5><b>Tanda Tangan</b></h5>
                        <h7>(Tanda Tangan Berupa Gambar)</h7><p></p>
                    </div>

                    <div align="center">
                        <div>
                            <h7><b>Pemohon (Customers)</b></h7>
                            <h7>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</h7>
                            <h7><b>Disetujui Oleh (Customers)</b></h7>
                        </div>
                        <div>
                            <input class="field" name="ttdcus1" type="file">
                            <input class="field" name="ttdcus1" type="file">
                            <br>
                            <br>
                            <br>
                            <br><br>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-6 col-md-offset-4" align="center">
                            <button type="submit" class="btn btn-primary">
                                <i class="fa fa-btn fa-sign-in"></i> Lanjut
                            </button>                            
                        </div> 
                    </div>
0 likes
14 replies
jlrdw's avatar

Better to store just name in the database.

Christianus's avatar

@jirdw yup thats right bro my friend told me too but still not sure how to do that :(

Caprico's avatar

Here's what you can do as an overview:

Validation:

-You can specify with a Request object what kind of mime types you will allow through the form. Lookup laravel form validation and `ctrl+f mime`

DB: -store the orignal name -create a random name -check and store the mimetype (image/jpeg) -use soft deletes so that you can get the file back if it is "accidentally" deleted

Storage (depending on version, this is what I've done in 5.2):

  • if you are storing these on the local server you can store it using the storage facade with a function similar to Storage::put('file')->public(). this will store it in the storage/app/public directory by default

  • if you are running 5.3 run php artisan storage:link (this links the public folder to the directory mentioned earlier)

Calling an image:

  • in the model create a files method and in the controller query the file. (ex. $invoice = Invoice::findOrFail('id)->with('files');

  • then in your view you can call the file by doing something like {{ Storage::get($invoice->file)}}

there may need to be some more configuration on your part but at least this should give you an idea.

1 like
Christianus's avatar

@Caprico i'm using same version laravel like you too bro so let me try it first, hope you assist me if i found trouble on this case again, gimme time to try and so thanks

Christianus's avatar

@Caprico where should i call Storage::put('file')->public()

is it on my Controller, or Route, or else ?

Christianus's avatar

@jekinney the other problem is i cant get the package, dont know why, my composer require always error so i need help to do step by step

i've add this in my function it's not error but not stored too

$image = $request->file('ttdcus1'); if(!empty($image)){

$imageName = 'cus'.$guests->id.'.'.$request->file('ttdcus1')->getClientOriginalExtension();

$request->file('ttdcus1')->move(base_path().'/public/img/ttd',$imageName);

Caprico's avatar

You would call Storage::put in the controller's store method.

Christianus's avatar

@Caprico from the code i share, what should i add ? cause i call Storage::put on Controller but still not working :(

Christianus's avatar
Christianus
OP
Best Answer
Level 1

On my controller

        if($request->hasFile('ttdcus1')) {
        $imageTempName = $request->file('ttdcus1')->getPathname();
            $imageName = $request->file('ttdcus1')->getClientOriginalName();
            $path = base_path() . '/public/img/ttd';
            $request->file('ttdcus1')->move($path , $imageName);
            }
        $keluhan = keluhan::create($request->all());
        $keluhan->ttd_pemohon = $imageName;
        $keluhan->save();

Solved. Thank You Guys for all information.

2 likes

Please or to participate in this conversation.