略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: finfo_buffer

2024-11-15

finfo_buffer

finfo::buffer

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL fileinfo >= 0.1.0)

finfo_buffer -- finfo::buffer返回一个字符串缓冲区的信息

说明

过程化风格

finfo_buffer(
    resource $finfo,
    string $string = null,
    int $options = FILEINFO_NONE,
    resource $context = null
): string

面向对象风格

public finfo::buffer(string $string = null, int $options = FILEINFO_NONE, resource $context = null): string

本函数用来获取字符串中二进制数据的信息。

参数

finfo

finfo_open() 函数返回的 Fileinfo 资源。

string

要检查的文件内容。

options

一个 Fileinfo 常量 或多个 Fileinfo 常量 进行逻辑或运算。

context

返回值

返回 string 参数所指定内容的类型描述。 发生错误时返回 false

范例

示例 #1 finfo_buffer() 函数例程

<?php
$finfo 
= new finfo(FILEINFO_MIME);
echo 
$finfo->buffer($_POST["script"]) . "\n";
?>

以上例程的输出类似于:

application/x-sh; charset=us-ascii

参见

add a noteadd a note

User Contributed Notes 4 notes

up
20
contact at ingnu dot fr
11 years ago
You can easily check mime type of an internet resource using this code :

<?php
function getUrlMimeType($url) {
   
$buffer = file_get_contents($url);
   
$finfo = new finfo(FILEINFO_MIME_TYPE);
    return
$finfo->buffer($buffer);
}
?>

I'm using it to detect if an url given by a user is a HTML page (so I do some stuff with the HTML) or a file on Internet (so I show an icon accordingly to the mime type).
up
4
nimasdj [AT] yahoo [DOT] com
6 years ago
You should never rely on finfo::buffer to get the MimeType of a file, you must always save the file physically or temporariliy and use finfo_open to get MimeType. I tested it with an excell file, with buffer it says octet-stream that is not valid, with finfo_open it says ms-excell as correct.
up
-18
donjajo4all at gmail dot com
8 years ago
Get only a webpage content type and forget the charset

<?php
$file
= new finfo(FILEINFO_MIME);
echo
strstr($file->buffer(file_get_contents('http://example.com')),';',true)//Returns something similar to  text/html
?>
up
-18
donjajo4all at gmail dot com
8 years ago
Get only a webpage content type and forget the charset

<?php
$file
= new finfo(FILEINFO_MIME);
echo
strstr(file_get_contents('http://example.com'),';',true); //Returns something similar to  text/html
?>

官方地址:https://www.php.net/manual/en/function.finfo-buffer.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3