通过JS Laravel访问存储目录 [英] Access storage directory through JS Laravel
问题描述
是否有一种方法可以访问 storage
目录,该目录已经链接到JS中的 public
目录?
Is there is a way to access the storage
directory, which is already linked to the public
directory in JS?
我正在尝试制作一张上传图片表格.验证脚本:
I'm trying to make an upload image form. Validation script:
if ($request->hasFile('photos')) {
$marker->photos = $request->photos->store('uploads');
}
它将图像保存在/storage/app/uploads/
我无法使用此图像,因为网站位于 public
文件夹中,并且无法访问任何其他文件夹,所以我做了 php artisan storage:link
创建到存储文件夹的子链接.
I can't use this image, because website is in public
folder, and can't have access to any other folders, so I did php artisan storage:link
to create a sublink to storage folder.
现在,如何使用1)PHP 2)JS访问图像?
Now, how can I access the image using 1) PHP 2) JS?
推荐答案
是的,您必须使用公用磁盘来存储文件:
Yes, you have to use the public disk to store the file:
if ($request->hasFile('photos')) {
$marker->photos = Storage::disk('public')->putFile('uploads', $request->file('photos'));
}
此语法类似于您使用的语法,也可以指定公共"磁盘:
This syntax, similar to the one you are using, it's also possible to specify 'public' disk:
$marker->photos = $request->file('photos')->store(
'uploads', 'public'
);
然后您就可以通过网址访问它了.
Then you'll can access to it by the url.
Laravel:
$url = asset('storage/uploads/filename.png');
$url = asset( Storage::url('uploads/filename.png') );
JS:
let imgSrc = "http://yourdomain/storage/uploads/filename.png";
参考:
文件存储文件上传.
文件存储公用磁盘.
文件存储文件URL .
这篇关于通过JS Laravel访问存储目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!