0

So I have a folder inside my storage called statuses which are photos uploaded, they can be private or public, so I can't upload them to the 'public folder'

I am doing a simple uppload like so:

    // If they have uploaded image(s)
    foreach($request->photos ?? [] as $photo){

        // Store the file
        $saveFile = Storage::putFile('status/' . $status->id, $photo, 'public');

        $photoAttributes = [
            'name'      => $photo->getClientOriginalName(),  
            'extension' => $photo->getClientOriginalExtension(),  
            'path'      => $saveFile, 
            'size'      => $photo->getSize(),  
            'mime'      => $photo->getMimeType(),  
        ];

        $create = $status
            ->images()
            ->save(new $this->photos($photoAttributes));
    }

Which uploads the file to /status/{statusid}/file

Then I save the info in the database like so:

    Schema::create('files', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('extension');
        $table->string('path');
        $table->string('size');
        $table->string('mime');
        $table->integer('owner_id');
        $table->string('owner_type');
        $table->timestamps();
        $table->softDeletes();
    });

Test data

INSERT INTO `social`.`files` 
    (`id`, 
    `name`, 
    `extension`, 
    `path`, 
    `size`, 
    `mime`, 
    `owner_id`, 
    `owner_type`, 
    `created_at`, 
    `updated_at`, 
    `deleted_at`) 
VALUES      
    ('2', 
    '2m2klhq1mzzz.jpg', 
    'jpg', 
    'status/2/iu4KiozJGNRdSMSju40T1UVVnop3bWZSRddXYIe8.jpeg', 
    '48033', 
    'image/jpeg', 
    '2', 
    'App\\Modules\\Timeline\\Entities\\Timeline', 
    '2018-01-09 14:54:31', 
    '2018-01-09 14:54:31', 
    NULL); 

The file is also being saved in the folder:

enter image description here

Now, when I want to display it, I am using the minme type and returning the files like so:

/**
 * ------------------
 * File routes
 * ------------------
 */
Route::resource('photos', 'PhotosController');


/**
 * Display the specified resource.
 *
 * @param  \App\Modules\Files\Entities\Photos  $photo
 * @return \Illuminate\Http\Response
 */
public function show(Photos $photo)
{
    $this->authorize('view', $photo);

    return (new Response(Storage::get($photo->path), 200))
        ->header('Content-Type', $photo->mime);
}

When I visit the page to see the image, I am getting a small white square like so:

enter image description here

Response when I dd the stuff inside the show method

https://pastebin.com/UbzJRRZv

How can I fix this?

2 Answers2

0

Which Laravel version you using?

If you are on >= 5.2 you can try response()->file Laravel Doc

return response()->file($pathToFile, $headers);
Seva Kalashnikov
  • 4,262
  • 2
  • 21
  • 36
0

I do it like that

My route

Route::get('photo/{delivery_photoEntity}/show', 'Operations\Delivery_photoController@show')->name('Delivery_photo.Show');

My controller

class Delivery_photoController
{
    public function show(Delivery_photoEntity $delivery_photoEntity)
    {
        return response()->file($delivery_photoEntity->path);
    }
}

My view

<img src="{{ route('Delivery_photo.Show', $photo) }}" style="max-width: 100%; max-height: 300px;">