2021-02-26 16:16:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Safe;
|
|
|
|
|
|
|
|
use Safe\Exceptions\FileinfoException;
|
|
|
|
|
|
|
|
/**
|
2022-07-12 19:26:21 +00:00
|
|
|
* This function closes the instance opened by finfo_open.
|
2021-02-26 16:16:17 +00:00
|
|
|
*
|
2022-07-12 19:26:21 +00:00
|
|
|
* @param resource $finfo An finfo instance, returned by finfo_open.
|
2021-02-26 16:16:17 +00:00
|
|
|
* @throws FileinfoException
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function finfo_close($finfo): void
|
|
|
|
{
|
|
|
|
error_clear_last();
|
|
|
|
$result = \finfo_close($finfo);
|
|
|
|
if ($result === false) {
|
|
|
|
throw FileinfoException::createFromPhpError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Procedural style
|
|
|
|
*
|
2022-07-12 19:26:21 +00:00
|
|
|
* Object-oriented style (constructor):
|
2021-02-26 16:16:17 +00:00
|
|
|
*
|
2022-07-12 19:26:21 +00:00
|
|
|
* This function opens a magic database and returns its instance.
|
2021-02-26 16:16:17 +00:00
|
|
|
*
|
2022-07-12 19:26:21 +00:00
|
|
|
* @param int $flags One or disjunction of more Fileinfo
|
2021-02-26 16:16:17 +00:00
|
|
|
* constants.
|
2022-07-12 19:26:21 +00:00
|
|
|
* @param string $magic_database Name of a magic database file, usually something like
|
2021-02-26 16:16:17 +00:00
|
|
|
* /path/to/magic.mime. If not specified, the
|
|
|
|
* MAGIC environment variable is used. If the
|
|
|
|
* environment variable isn't set, then PHP's bundled magic database will
|
|
|
|
* be used.
|
|
|
|
*
|
|
|
|
* Passing NULL or an empty string will be equivalent to the default
|
|
|
|
* value.
|
|
|
|
* @return resource (Procedural style only)
|
2022-07-12 19:26:21 +00:00
|
|
|
* Returns an finfo instance on success.
|
2021-02-26 16:16:17 +00:00
|
|
|
* @throws FileinfoException
|
|
|
|
*
|
|
|
|
*/
|
2022-07-12 19:26:21 +00:00
|
|
|
function finfo_open(int $flags = FILEINFO_NONE, string $magic_database = null)
|
2021-02-26 16:16:17 +00:00
|
|
|
{
|
|
|
|
error_clear_last();
|
2022-07-12 19:26:21 +00:00
|
|
|
if ($magic_database !== null) {
|
|
|
|
$result = \finfo_open($flags, $magic_database);
|
|
|
|
} else {
|
|
|
|
$result = \finfo_open($flags);
|
|
|
|
}
|
2021-02-26 16:16:17 +00:00
|
|
|
if ($result === false) {
|
|
|
|
throw FileinfoException::createFromPhpError();
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the MIME content type for a file as determined by using
|
|
|
|
* information from the magic.mime file.
|
|
|
|
*
|
2022-07-12 19:26:21 +00:00
|
|
|
* @param string|resource $filename Path to the tested file.
|
2021-02-26 16:16:17 +00:00
|
|
|
* @return string Returns the content type in MIME format, like
|
|
|
|
* text/plain or application/octet-stream.
|
|
|
|
* @throws FileinfoException
|
|
|
|
*
|
|
|
|
*/
|
2022-07-12 19:26:21 +00:00
|
|
|
function mime_content_type($filename): string
|
2021-02-26 16:16:17 +00:00
|
|
|
{
|
|
|
|
error_clear_last();
|
|
|
|
$result = \mime_content_type($filename);
|
|
|
|
if ($result === false) {
|
|
|
|
throw FileinfoException::createFromPhpError();
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|