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

nitz25's avatar

Retrieve image from database

Hello, I managed to save image to database and my problem now is how can I retrieve the image in my view? I am not using Image Intervention just a simple code only.. This is my Controller

$data = $request->all();
            if(Input::hasFile('file')){
                $file = Input::file('file');
                $filename = $file->getClientOriginalName();
                $image = Image::create([
                    'image_type' => $data['image_type'],
                    'link_id' => $data['link_id'],    
                    'image_filename' => $filename,
                    'image_file' => $file,
                    'image_size'=>$file->getSize(),
                    'image_mime'=>$file->getMimeType(),
                    'auid' => Auth::user()->id,
                ]);            
            }

and to my migration / table

 Schema::create('images', function (Blueprint $table) {
            $table->increments('id');
            $table->string('image_type', 4);
            $table->string('image_filename', 100);
            $table->binary('image_file');
            $table->integer('image_size');
            $table->string('image_mime');
            $table->integer('link_id');
            $table->integer('auid')->nullable()->default(null);
            $table->integer('uuid')->nullable()->default(null);
            $table->timestamps();
        });

In my view when I execute the project what I get is "/tmp/phpHbFo3m"

@foreach($images as $image)
    {{$image->image_file}}
@endforeach

And also I can already pass the data to my view, all I want to know now is how to retrieve the image in my view.. Any help will be appreciated :)

0 likes
9 replies
mikebarwick's avatar

Lets see you controller method where you're generating your viewing and passing in $images...

nitz25's avatar

This is it I just put all my codes.

    public function hotelImage($id){
        if (Auth::check()) {
            $utilities = new SAUtilities;
            $hotel = Hotel::findorfail($id);
            $images = $utilities->getImages("H", $id);

            return view('maintenance.booking_site.image_form', ['image_type' => 'H', 'hotel' => $hotel, 'images' => $images]);
      
       } else {
            return redirect()->guest('login');
        }
    }
Reached's avatar

Hi @nitz25,

Actually you are referencing a path to an image, and not an actual image file - just for clearing that up :).

"/tmp/phpHbFo3m" <--- you wont be able to use this in your view as it is now, since there is no file type attached to it.

I wouldn't probably do this myself, but you can try to do the following and attach the mime type to the picture:

@foreach($images as $image)
    <img src="{{$image->image_file}}{{$image->image_mime}}">
@endforeach

I would use the intervention package though, as it takes care of all this for you.

willvincent's avatar

In my view when I execute the project what I get is "/tmp/phpHbFo3m"

That sounds like the DB is giving you the stored value just fine, and the problem is not, in fact with fetching data from the database but rather you are missing the step of actually storing the file(s) on completion of upload. Uploads go to /tmp, you then need to move those to your actual file storage directory.

Refer to the bulk file uploads part of the project flyer series, starting around 7:00 for handling of file uploads. It definitely sounds like you're missing a step or two.

1 like
nitz25's avatar

@willvincent sorry Im just a student and I cant watch the tutorial cause I dont have credit card applied.

nitz25's avatar
nitz25
OP
Best Answer
Level 2

Thank you for your response, I got wrong in upload image and viewing, and fixed it already so here is my codes.. Controller

 if(Input::hasFile('file')){
                $file = Input::file('file');
                $filename = $file->getClientOriginalName();
                $image = Image::create([
                    'image_type' => $data['image_type'],
                    'link_id' => $data['link_id'],    
                    'image_filename' => $filename,
                    'image_file' => base64_encode(file_get_contents($file->getRealPath())), //added codes
                    'image_size'=>$file->getSize(),
                    'image_mime'=>$file->getMimeType(),
                    'auid' => Auth::user()->id,
                ]);            
            }

and in my view

  @foreach($images as $image)
    <img src="data:{{$image->image_mime}};base64,{{$image->image_file}}"/>
 @endforeach

Please or to participate in this conversation.