laravel - 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文件夹。我在这里错过了什么吗?
解决方案
看来我不能发表评论作为答案。所以让我添加我的输入。您是否创建了符号链接?如果现在不做,或者只是重做。当我将代码从 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 是文件输入的名称。
推荐阅读
- c++ - 如何在 node-addon-api 中从 c++ 返回 float32 数组?
- ios - 模拟器冻结 | SwiftUI、XCode
- python - python pandas.read_excel .xls 不支持的格式,或损坏的文件
- testing - 我们如何从 testcafe 自动化案例中生成的 url 中删除会话 ID
- javascript - 如何在 nextjs 中获取动态页面 html 输出?
- php - 如何在laravel的database.php文件中动态附加连接
- django - Django CustomUser 使 emailfield 成为参考
- agora.io - 如何使用 Agora.IO 视频 SDK 记录和稍后检索数据跟踪/消息(基于文本)?
- wordpress - 我想要最新的评论和 WordPress 中博客文章的缩略图
- java - Java 将时间转换为 iso_instant