Laravel上传的图像未显示,但链接存储正常工作。

huangapple go评论91阅读模式
英文:

Laravel Images uploaded are not showing but link storage is working

问题

我目前正在将2个项目上传到我的Ubuntu服务器上,使用CyberPanel。(其中一个是https://simplycar.es/storage/fotos/1677542768-WhatsApp%20Image%202023-02-28%20at%2001.03.28.jpeg)

这两个项目都使用Laravel,并且使用相同的方式来上传图片。

上传方式:

if (is_uploaded_file($request->file('foto'))) {
    $picture = time() . "-" . $request->file('foto')->getClientOriginalName();
    $request->file('foto')->storeAs('public/fotos', $picture);
}

我已经有另一个项目在运行,它使用相同的结构来上传图片,并且正常工作(https://chaincapitals.com/storage/logos/1669744070-C5l3mHJg_400x400.png)。

但这两个新项目无法正常使用已上传的图片。它们不会出现在URL中(应用程序URL/图片名称),也不会显示在网站上。

我按照通常的方式编写图片的路由:

{{ asset('storage/fotos/' . \App\Models\Image::where('idCar', $car->id)->first()->imagen) }}

存储链接正常工作,因为在我的FTP应用程序中可以看到。

我尝试重新生成符号链接,也尝试从一个PHP文件中生成它。

此外,我尝试了一些权限相关的操作,例如:

chmod -R o+w storage
chmod -R o+w bootstrap/cache

我还尝试了一些其他我在其他主题中看到的命令。

英文:

I'm currently uploading 2 projects into my ubuntu server with cyberpanel. (One of them is https://simplycar.es/storage/fotos/1677542768-WhatsApp%20Image%202023-02-28%20at%2001.03.28.jpeg)

Both are in Laravel and use the same way to upload images.

Way to upload:

if (is_uploaded_file($request->file('foto'))) {
        $picture = time() . "-" . $request->file('foto')->getClientOriginalName();
        $request->file('foto')->storeAs('public/fotos', $picture);
    }

I already have another project running with the same structure to upload pictures, and it works correctly (https://chaincapitals.com/storage/logos/1669744070-C5l3mHJg_400x400.png).

But these two new projects don't work with the uploaded images. They don't appear in the URL (app. URL/(image-name)) and they don't show on the website.

I'm writing the route of the images as I always do it:

{{asset('storage/fotos/'. \App\Models\Image::where('idCar' , $car->id)->first()->imagen)}}

The storage link is working because, in my FTP App, it appears.

I tried to regenerate the symlink also generate it from a .PHP file.

Also, I tried with permissions things, I try this two things:

chmod -R o+w storage
chmod -R o+w bootstrap/cache

Also, I triod some other commands I saw on other topics.

答案1

得分: 1

我不确定,但可以猜测一下,请检查您的权限,因为您说它在另一个项目上可以工作。

sudo chown -R www-data:www-data storage/
sudo chmod -R 775 storage/

# 最后
php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan view:clear
英文:

I'm not sure, certainly. As a wild guess, check your permission since you say it worked on another project.

sudo chown -R www-data:www-data storage/
sudo chmod -R 775 storage/

# finally 
php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan view:clear

答案2

得分: 0

根据@Abdula Nilam的回答,我收到了以下错误信息:

无法以附加模式打开流或文件"/home/simplycar.es/public_html/storage/logs/laravel.log":无法打开流:

我认为这是因为存储文件夹的权限问题。

我用以下方法修复了它:

sudo chmod -R ugo+rw storage

之后,我的应用程序正常运行。

英文:

Following the answer of @Abdula Nilam I got the following error:

> The stream or file "/home/simplycar.es/public_html/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream:

I assume is because of permissions of the storage folder.

I fixed it with:

sudo chmod -R ugo+rw storage

After this I got my app working correctly.

huangapple
  • 本文由 发表于 2023年3月1日 16:24:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75601140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定