javascript – Laravel Get File Contents of Image but Not Working on Production Environment-ThrowExceptions

Exception or error:

My code bellow working on localhost development

<img class="img-fluid rounded-circle" src="{{ url('image?type=user&file='.Auth::user()->picture_file.'&mime='.Auth::user()->picture_mime) }}" alt="Image Description">

will access imageController with get method using this link image?type=user&file=USER000053.png&mime=image/png

class ImageController extends BaseController
{
    public function get(Request $request){
        $file=Storage::disk($request->type)->get($request->file); 
        return response($file,200)->header('content-Type',$request->mime);
    }
}

bellow my filesystem.php config file

'user' => [
    'driver' => 'local',
    'root' => storage_path('app/user'),
    'url' => env('APP_URL').'/storage',
    'visibility' => 'public',
],

that code above works well on localhost. but when we tried to push to production server (centos os), that code won’t works. Its just returning a damage image.

we have tried

  1. [check] make sure storage and temp accessible. chmod 755. even we have tried 777
  2. [check] chown to user apache.apache
  3. [check] set selinux permisive mode
  4. [check] we have make sure that file already exists

    $file_exists = Storage::disk($request->type)->exists($request->file);
    

    the code above return true

  5. [check] trying this code bellow

    $response = Response::make($file, 200);
    $response->header('Content-Type', $request->mime);
    return $response;
    

    still won’t work

  6. trying to make symlink

    php artisan storage:link
    

    its makes symlink on public folder but still won’t work

  7. trying to download and still doesn’t works

     return response()->download(storage_path('app/' . $request->type . "/" . $request->file));
    

I’m running on Centos 7 Environment at GCP. any idea would be help me. thank you

update 1
Here are the result from our server

enter image description here

update 2
I have already check for that image on server and its fine and could be read

update 3

We have do this

$img = file_get_contents(public_path("storage/user/" . $request->file));
return response($img)->header('Content-type','image/png');

still not working

update 4
we make our own symbolic link on public folder as bellow
storage -> /var/www/html/*****/storage/app/

still not working

update 5
I have directly accessing file through browser, it comes up with the picture.

update 6
Have already set FILESYSTEM_DRIVER=local or FILESYSTEM_DRIVER=useron env file and still doesn’t works

update 7

filesystem.php

 'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

    'calculate_disk' => [
        'driver' => 'local',
        'root' => storage_path('app/calculate'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

    'user' => [
        'driver' => 'local',
        'root' => storage_path('app/user'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

    'invoice' => [
        'driver' => 'local',
        'root' => storage_path('app/invoice'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'url' => env('AWS_URL'),
    ],

],

web.php

Route::get('/image', 'ImageController@get');

update 8

after digging a while of FileSystemDriver I was curious about bellow url value that linked to localhost instead of our url. any idea?

FilesystemAdapter {#479 ▼
  #driver: Filesystem {#482 ▼
    #adapter: Local {#475 ▼
      #pathSeparator: "/"
      #permissionMap: array:2 [▼
        "file" => array:2 [▼
          "public" => 420
          "private" => 384
        ]
        "dir" => array:2 [▼
          "public" => 493
          "private" => 448
        ]
      ]
      #writeFlags: 2
      -linkHandling: 2
      #pathPrefix: "/var/www/html/***/storage/app/user/"
    }
    #plugins: []
    #config: Config {#477 ▼
      #settings: array:2 [▼
        "url" => "http://localhost/storage"
        "visibility" => "public"
      ]
      #fallback: null
    }
  }
}
How to solve:

Just add ob_get_clean(); before response file.

It will clean all buffers and return content to display image.

Leave a Reply

Your email address will not be published. Required fields are marked *