如何使用 Laravel Facades 检查文件类型?
有没有一种方便的方法来使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以对上传的文件使用 getClientMimeType() 方法。
You can use getClientMimeType() method on uploaded files.
您可以使用 PHP 的内置函数 getimagesizefromstring 从图像中获取图像 mime字符串/base64 为:
You can use the PHP's inbuilt function getimagesizefromstring to get image mime from image string/base64 as:
文件类型是什么意思?
您可以使用以下代码从 base64 中提取扩展,我认为 Laravel 没有任何内置方法。
您还可以使用 图像包 来创建和操作图像来自 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.
Also you can use something like Image package to create and manipulate the image from base64 string.