Home >Backend Development >PHP Tutorial >How to get the mime type of a file in PHP? (code example)

How to get the mime type of a file in PHP? (code example)

青灯夜游
青灯夜游Original
2019-03-19 14:23:336904browse

In PHP, you can use the built-in function mime_content_type() function to obtain the mime type of the file. The following article will take you to understand the mime_content_type() function and see how the mime_content_type() function obtains the mime type of the file. .

How to get the mime type of a file in PHP? (code example)

me_content_type() function is a built-in function in PHP that is used to obtain the MIME content type of a file.

Basic syntax:

string mime_content_type($file)

Parameters: The me_content_type() function accepts a single parameter $file, which specifies what to look for File path for MIME details.

Return value: The me_content_type() function returns the MIME content type; if it fails, it will return false.

Example:

The following are some files in the demo directory. Let’s use the me_content_type() function to get the mime types of these files:

How to get the mime type of a file in PHP? (code example)

Implementation code:

<?php 
// 输出不同类型文件的结果
echo mime_content_type(&#39;demo/1.jpg&#39;) . "</br>"; 
echo mime_content_type(&#39;demo/2.png&#39;) . "</br>"; 
echo mime_content_type(&#39;demo/tp.psd&#39;) . "</br>"; 
echo mime_content_type(&#39;demo/index.html&#39;) . "</br>"; 
echo mime_content_type(&#39;demo/sqpurple.gif&#39;) . "</br>"; 
echo mime_content_type(&#39;demo/filename.txt&#39;) . "</br>"; 
echo mime_content_type(&#39;demo/demo.ppt&#39;) . "</br>"; 
echo mime_content_type(&#39;demo/filename.pdf&#39;) . "</br>"; 
echo mime_content_type(&#39;demo/filename.docx&#39;) . "</br>"; 
?>

Output:

How to get the mime type of a file in PHP? (code example)

##Related video tutorial recommendation: "

PHP Tutorial

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to get the mime type of a file in PHP? (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more