如何使用 Laravel Facades 检查文件类型?

发布于 2025-01-16 19:47:54 字数 995 浏览 2 评论 0原文

有没有一种方便的方法来使用 File 外观检查文件的文件类型?我可以像这样检查文件扩展名

$ext = File::extension('623d91b094472.png');

我知道我可以从文件扩展名派生文件类型,但这需要一些条件字符串操作,所以想知道是否有一种更方便的方法可以从诸如 File::type(...) 之类的 facade 获取它。

我需要 file type 因为我在 Vue.js 中编码文件并在 Laravel 中解码它们,这需要替换 base64 字符串中的>文件类型文件扩展名

$file64 = str_replace('data:image/png;base64,', '', $request->file);

我已经尝试过这个:

$type = File::type('623d91b094472.png');

但它抛出:

local.ERROR: filetype(): Lstat failed for 623d91b094472.png {"userId":16,"exception":"[object] (ErrorException(code: 0): filetype(): Lstat failed for 623d91b094472.png at /Users/artur/PhpstormProjects/safa-ameedee.com/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php:433)
[stacktrace]

Is there a convenient way to check the file type of a file with the File facade? I can check the file extension like so:

$ext = File::extension('623d91b094472.png');

I know I can derive the file type from the file extension but it would require some conditional string manipulation, so was wondering if there is a more convenient way to just get it from a facade like File::type(...) or such.

I need the file type because I'm encoding files in Vue.js and decoding them in Laravel, which requires the replacement of the file type and file extension in the base64 string:

$file64 = str_replace('data:image/png;base64,', '', $request->file);

I've tried this:

$type = File::type('623d91b094472.png');

but it throws:

local.ERROR: filetype(): Lstat failed for 623d91b094472.png {"userId":16,"exception":"[object] (ErrorException(code: 0): filetype(): Lstat failed for 623d91b094472.png at /Users/artur/PhpstormProjects/safa-ameedee.com/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php:433)
[stacktrace]

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

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

发布评论

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

评论(3

甜尕妞 2025-01-23 19:47:54

您可以对上传的文件使用 getClientMimeType() 方法。

<?php $file->getClientMimeType();

You can use getClientMimeType() method on uploaded files.

<?php $file->getClientMimeType();
残龙傲雪 2025-01-23 19:47:54

您可以使用 PHP 的内置函数 getimagesizefromstring 从图像中获取图像 mime字符串/base64 为:

$imageData = getimagesizefromstring($data);

$mime = $imageData['mime']; // image/png
$ext = explode('/', $mime)[1]; // png

echo $ext; //png

You can use the PHP's inbuilt function getimagesizefromstring to get image mime from image string/base64 as:

$imageData = getimagesizefromstring($data);

$mime = $imageData['mime']; // image/png
$ext = explode('/', $mime)[1]; // png

echo $ext; //png
一刻暧昧 2025-01-23 19:47:54

文件类型是什么意思?

您可以使用以下代码从 base64 中提取扩展,我认为 Laravel 没有任何内置方法。

<?php

$base64string = "data:image/png;base64,...";
$fileExt = explode('/', explode(':', substr($base64string, 0, strpos($base64string, ';')))[1])[1];

echo $fileExt; // returns "png"

$fileType = explode('/', explode(':', substr($base64string, 0, strpos($base64string, ';')))[1])[0];

echo $fileType; // returns "image"

您还可以使用 图像包 来创建和操作图像来自 base64 字符串。

What do you mean by file type?

You can extract the extensions from base64 with the following code, I don't think Laravel has any builtin methods for this.

<?php

$base64string = "data:image/png;base64,...";
$fileExt = explode('/', explode(':', substr($base64string, 0, strpos($base64string, ';')))[1])[1];

echo $fileExt; // returns "png"

$fileType = explode('/', explode(':', substr($base64string, 0, strpos($base64string, ';')))[1])[0];

echo $fileType; // returns "image"

Also you can use something like Image package to create and manipulate the image from base64 string.

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