在Laravel Orchid中获取上载文件附件的相对URL?

发布于 2025-02-06 02:06:29 字数 706 浏览 0 评论 0原文

您好,我正在使用Laravel Orchid添加功能将文件上传到服务器。我将(部分)URL发送到存储在硬盘驱动器上的文件中。 我正在使用提供完整URL字符串的$ attactment-> url()检索文件URL,但我想知道如何获取文件的相对URL。

The code I have looks similar to

public function uploadPhoto(Request $request)
{
    $file = new File($request->file('fileData'));
    $attachment = $file->load();

    error_log("Uploaded url". $attachment->url());

    return response()->json();
}

This outputs:

http://localhost/storage/2022/06/08/247437ba9e73954388a4a350fc0f292409a679d4.jpg

But I want to get a relative URL:

/storage/2022/06/08/247437ba9e73954388a4a350fc0f292409a679d4.jpg

How can我从Laravel Orchid的附件上获得了上传文件的相对URL?

Hello I am adding the functionality to upload files to the server using the laravel orchid. I am sending the (partial) URL to the file stored on the hard-drive back to the frontend.
I am retrieving the file URL using the $attachment->url() that gives a full URL string but I would like to know how to get a relative URL to the file.

The code I have looks similar to

public function uploadPhoto(Request $request)
{
    $file = new File($request->file('fileData'));
    $attachment = $file->load();

    error_log("Uploaded url". $attachment->url());

    return response()->json();
}

This outputs:

http://localhost/storage/2022/06/08/247437ba9e73954388a4a350fc0f292409a679d4.jpg

But I want to get a relative URL:

/storage/2022/06/08/247437ba9e73954388a4a350fc0f292409a679d4.jpg

How can I get a relative URL to the uploaded file from the attachment in Laravel orchid?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花间憩 2025-02-13 02:06:30

要获取相对于公共文件夹使用的URL

$attachment->relativeUrl 

,或者

parse_url($attachment->url(), PHP_URL_PATH)

注意$ actionment-> url()还进行额外检查以确认文件是否存在于硬盘驱动器上。

url()方法将首先检查存在路径,然后获取
URL。当使用S3之类的外部存储时,这将使两个
呼叫。为了提高性能,您可以使用缓存适配器
由Laravel推荐以提高性能。你也可以简单
覆盖此方法并适应您的需求。
兰花附件

To get the url relative to the public folder use

$attachment->relativeUrl 

or

parse_url($attachment->url(), PHP_URL_PATH)

Note that $attachment->url() additionally does an extra check to confirm that the file exists on the hard drive.

The url() method will first check for the path existence, and then get
the URL. When using external storage like s3, this will make two
calls. To improve performance you can use the caching adapter
recommended by Laravel to improve performance. You can also simply
override this method and adjust to your needs.
Orchid Attachments

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文