首页 > 解决方案 > Laravel:上传文件夹在错误的目录中创建

问题描述

我有一个准备保存文件输入的表单。

 $request->validate([
            'name'=> 'required|min:3',
            'image'=> 'required|image|mimes:jpeg,png,jpg'
        ]);

        $category = new Category();
        $category->name = $request->name;

        $path = $request->file('image')->store('categories_images');

        $category->image = $path;

上面的代码所做的是它从请求中获取图像字段并将其保存到categories_images文件夹。当我第一次上传文件进行测试时,它在storage/app中创建了文件夹。

我的问题是我想在我的网站上预览图片:

//store.state.serverPath returns: http://localhost:8000 -> this is right
<img :src="`${$store.state.serverPath}/storage/${category.image}`" class="image-wd"/>
 

当我在浏览器中检查它时,它说:

http://localhost:8000/storage/categories_images/adGR57Gq6lNUqRVvEubRDfxNMZzEhya3A7oESUox.png 未找到

它需要storage/app/public中的图像,但每次我在 storage/app中上传图像时都会创建categories_images文件夹。我在这里错过了什么吗?

标签: laravelvue.js

解决方案


看来我不能发表评论作为答案。所以让我添加我的输入。您是否创建了符号链接?如果现在不做,或者只是重做。当我将代码从 mac 移植到 windows 时,我遇到了同样的问题。即使所有配置都正确完成,链接也与我的 mac 相关。我现在刚刚为我的代码解决了这个问题。所以谢谢你的问题。

   php artisan storage:link

store 的第一个参数也是你要在 storage/public 目录下保存文件的路径。您需要在您的 url 中附加目录名称才能找到该文件。这是我在我的一个项目中使用的代码。

$path = isset($validated['document']) ? $path = $request->file('document')->store('pdfs', 'public') : null;

该文件将通过 URL www.example.com/storage/pdfs/$path访问

document 是文件输入的名称。


推荐阅读