Home > Software engineering >  Image File Can Not Be Read In View
Image File Can Not Be Read In View

Time:05-19

I'm working with Laravel 8 and I can properly upload images to storage/app/public/images/ but the image can not be loaded somehow in the view when I do this:

<img src="https://sitename.com/storage/app/public/images/{{ $product->image_path }}"> 

So what's going wrong here?

How can I fix this issue?

Note that I'm running this live on server in production and I don't have access to Terminal/Command Line for running Laravel Commands (No SSH also because of shared hosting).

CodePudding user response:

Add this code to your web.php file.

Route::get("/storage", function (){
    Artisan::call('storage:link');
});

Then visit https://yourwebsite.com/storage. You can now access the image.

CodePudding user response:

Try below code..

<img src="https://sitename.com/storage/images/{{ $product->image_path }}">

CodePudding user response:

It's upload photo. I think your image folder should be within public folder

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;

class FileUploadController extends BaseController
{
    public function uploadImage(Request $request)
    {
        $user = (object) ['image' => ""];

        if ($request->hasFile('image')) {
            $original_filename = $request->file('image')->getClientOriginalName();
            $original_filename_arr = explode('.', $original_filename);
            $file_ext = end($original_filename_arr);
            $destination_path = './upload/image/';
            $image = 'ME-' . time() . '.' . $file_ext;

            if ($request->file('image')->move($destination_path, $image)) {
                $user->image = '/upload/image/' . $image;
                return $this->responseRequestSuccess($user);
            } else {
                return $this->responseRequestError('Cannot upload file');
            }
        } else {
            return $this->responseRequestError('File not found');
        }
    }

    protected function responseRequestSuccess($ret)
    {
        return response()->json(['status' => 'success', 'data' => $ret], 200)
            ->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    }

    protected function responseRequestError($message = 'Bad request', $statusCode = 500)
    {
        return response()->json(['status' => 'error', 'error' => $message], $statusCode)
            ->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    }
}

After. {{APP_URL}}/upload/image/image.jpg It's working

  • Related