当项目上传到Laravel中的cpanel时,图像未显示

时间:2019-04-05 09:20:29

标签: laravel image hosting

我从实时网站上传的任何图像都没有显示...但是在localhost上运行良好,我也完成了php artisan storage:link,但结果仍然相同。图片正在保存在公用文件夹中

....

我已使用此逻辑保存图像

...

公共函数saveFiles(Request $ 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) }}"/>

1 个答案:

答案 0 :(得分:1)

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

您应该在公共目录中的index.php中设置公共路径

类似于以下代码:

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