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

青灯夜游
Release: 2023-04-05 16:48:01
Original
6859 people have browsed it

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>"; 
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template