在 laravel 中将项目上传到 cpanel 时图像未显示

Image not showing while project uploaded to cpanel in laravel

我从实时网站上传的任何图片都没有显示...但是在本地主机上工作正常我也做了 php artisan storage:link 但结果仍然相同。图片保存在 public 文件夹

.....

我用这个逻辑保存图片

...

public 函数 saveFiles(请求 $request) {

    $uploadPath = public_path(env('UPLOAD_PATH'));
    $thumbPath = public_path(env('UPLOAD_PATH').'/thumb');
    if (! file_exists($uploadPath)) {
        mkdir($uploadPath, 0775);
        mkdir($thumbPath, 0775);
    }

    $finalRequest = $request;

    foreach ($request->all() as $key => $value) {
        if ($request->hasFile($key)) {
            if ($request->has($key . '_max_width') && $request->has($key . '_max_height')) {
                // Check file width
                $filename = time() . '-' . $request->file($key)->getClientOriginalName();
                $file     = $request->file($key);
                $image    = Image::make($file);
                if (! file_exists($thumbPath)) {
                    mkdir($thumbPath, 0775, true);
                }
                Image::make($file)->resize(50, 50)->save($thumbPath . '/' . $filename);
                $width  = $image->width();
                $height = $image->height();
                if ($width > $request->{$key . '_max_width'} && $height > $request->{$key . '_max_height'}) {
                    $image->resize($request->{$key . '_max_width'}, $request->{$key . '_max_height'});
                } elseif ($width > $request->{$key . '_max_width'}) {
                    $image->resize($request->{$key . '_max_width'}, null, function ($constraint) {
                        $constraint->aspectRatio();
                    });
                } elseif ($height > $request->{$key . '_max_height'}) {
                    $image->resize(null, $request->{$key . '_max_height'}, function ($constraint) {
                        $constraint->aspectRatio();
                    });
                }
                $image->save($uploadPath . '/' . $filename);
                $finalRequest = new Request(array_merge($finalRequest->all(), [$key => $filename]));
            } else {
                $filename = time() . '-' . $request->file($key)->getClientOriginalName();
                $request->file($key)->move($uploadPath, $filename);
                $finalRequest = new Request(array_merge($finalRequest->all(), [$key => $filename]));
            }
        }
    }

    return $finalRequest;
}

...

...

我的config/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',
    ],

    's3' => [
        'driver' => 's3',
        'key'    => env('AWS_KEY'),
        'secret' => env('AWS_SECRET'),
        'region' => env('AWS_REGION'),
        'bucket' => env('AWS_BUCKET'),
    ],

    'uploads' => [
        'driver' => 'local',
        'root' => public_path('uploads'),
    ],

    'uploads_test' => [
        'driver' => 'local',
        'root' => public_path('uploads/test')
    ],

],

用这个来展示我的照片

<img src="{{ asset(env('UPLOAD_PATH').'/' . $room->display_image) }}"/>

我认为这是因为在本地,public 文件夹与项目的其他文件夹 (app-bootstrap-config-....) 在同一目录中,但在共享主机中那些项目文件夹不在 public 文件夹附近(例如 public_html)

您应该在 public

中的 index.php 中设置您的 public 路径

类似于下面的代码:

$app->bind('path.public', function() {
    return __DIR__;
});