nqkk58 发表于 2024-10-4 20:01:55

判断文件类型的PHP函数


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在PHP中,<span style="color: black;">咱们</span>经常需要<span style="color: black;">按照</span>文件的类型来进行处理,<span style="color: black;">例如</span><span style="color: black;">针对</span><span style="color: black;">区别</span>类型的<span style="color: black;">照片</span>进行缩放、裁剪等操作,在这种<span style="color: black;">状况</span>下,判断文件类型就<span style="color: black;">显出</span>非常重要。<span style="color: black;">那样</span>,本文将介绍几种常用的判断文件类型的PHP函数。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">mime_content_type函数</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在PHP5.3之前的版本中,<span style="color: black;">能够</span><span style="color: black;">运用</span>mime_content_type函数来获取文件的MIME类型。这个函数的语法如下:</p>string&nbsp;mime_content_type&nbsp;(&nbsp;string&nbsp;<span style="color: black;">$filename</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">该函数接受一个文件名<span style="color: black;">做为</span>参数,返回该文件的MIME类型。例如:</p><span style="color: black;">$filename</span>&nbsp;=&nbsp;<span style="color: black;">test.jpg</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p><span style="color: black;">$mime_type</span>&nbsp;=&nbsp;mime_content_type(<span style="color: black;">$filename</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p><span style="color: black;">echo</span>&nbsp;<span style="color: black;">"The&nbsp;MIME&nbsp;type&nbsp;of&nbsp;<span style="color: black;">$filename</span>&nbsp;is:&nbsp;<span style="color: black;">$mime_type</span>"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这个函数<span style="color: black;">能够</span>判断大部分<span style="color: black;">平常</span>类型的文件,<span style="color: black;">例如</span><span style="color: black;">照片</span>、音频、视频、文本等等。<span style="color: black;">然则</span>,它并不支持所有的文件类型。在某些<span style="color: black;">状况</span>下,它可能会返回错误的MIME类型。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">finfo_file函数</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">从PHP5.3<span style="color: black;">起始</span>,<span style="color: black;">能够</span><span style="color: black;">运用</span>finfo_file函数来获取文件的MIME类型。这个函数的语法如下:</p>finfo&nbsp;finfo_file&nbsp;(&nbsp;resource&nbsp;<span style="color: black;">$finfo</span>&nbsp;,&nbsp;string&nbsp;<span style="color: black;">$filename</span>&nbsp;[,&nbsp;int&nbsp;<span style="color: black;">$options</span>&nbsp;=&nbsp;FILEINFO_NONE&nbsp;[,&nbsp;resource&nbsp;<span style="color: black;">$context</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;]]&nbsp;)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其中,$filename是需要<span style="color: black;">检测</span>的文件名,而$finfo<span style="color: black;">是由于</span>finfo_open函数返回的文件信息对象。例如:</p><span style="color: black;">$finfo</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;=&nbsp;finfo_open(FILEINFO_MIME_TYPE);</p><span style="color: black;">$filename</span>&nbsp;=&nbsp;<span style="color: black;">test.jpg</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p><span style="color: black;">$mime_type</span>&nbsp;=&nbsp;finfo_file(<span style="color: black;">$finfo</span>,&nbsp;<span style="color: black;">$filename</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p><span style="color: black;">echo</span>&nbsp;<span style="color: black;">"The&nbsp;MIME&nbsp;type&nbsp;of<span style="color: black;">$filename</span>&nbsp;is:&nbsp;<span style="color: black;">$mime_type</span>"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">需要<span style="color: black;">重视</span>的是,finfo_file函数需要依赖PHP的fileinfo扩展,<span style="color: black;">因此呢</span>在<span style="color: black;">运用</span>之前需要<span style="color: black;">保证</span>该扩展已被启用。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">pathinfo函数</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">pathinfo函数是PHP自带的一个函数,<span style="color: black;">能够</span>用来获取一个文件的路径信息。除了文件的路径和文件名之外,它还<span style="color: black;">能够</span>返回文件的扩展名。这个函数的语法如下:</p>array&nbsp;pathinfo&nbsp;(&nbsp;string&nbsp;<span style="color: black;">$path</span>&nbsp;[,&nbsp;int&nbsp;<span style="color: black;">$options</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">=&nbsp;PATHINFO_DIRNAME&nbsp;|&nbsp;PATHINFO_BASENAME&nbsp;|&nbsp;PATHINFO_EXTENSION&nbsp;|&nbsp;PATHINFO_FILENAME&nbsp;]&nbsp;)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其中,$path是需要解析的文件路径。返回的结果是一个<span style="color: black;">包括</span>路径信息的数组,<span style="color: black;">包含</span>dirname、basename、extension和filename。例如:</p><span style="color: black;">$filename</span>&nbsp;=&nbsp;<span style="color: black;">test.jpg</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p><span style="color: black;">$info</span>&nbsp;=&nbsp;pathinfo(<span style="color: black;">$filename</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p><span style="color: black;">echo</span>&nbsp;<span style="color: black;">"The&nbsp;extension&nbsp;of<span style="color: black;">$filename</span>&nbsp;is:&nbsp;"</span>&nbsp;.&nbsp;<span style="color: black;">$info</span>[<span style="color: black;">extension</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">];</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">getimagesize函数</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span><span style="color: black;">咱们</span>要判断一个文件<span style="color: black;">是不是</span>为<span style="color: black;">照片</span>,<span style="color: black;">那样</span><span style="color: black;">能够</span><span style="color: black;">运用</span>getimagesize函数。这个函数的语法如下:</p>array|<span style="color: black;">false</span>&nbsp;getimagesize&nbsp;(&nbsp;string&nbsp;<span style="color: black;">$filename</span>&nbsp;[,&nbsp;array&nbsp;&amp;<span style="color: black;">$imageinfo</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;]&nbsp;)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其中,$filename是需要<span style="color: black;">检测</span>的文件名,$imageinfo是一个可选的数组,用于在获取<span style="color: black;">照片</span>信息时存储额外的数据。<span style="color: black;">倘若</span>getimagesize函数返回false,则说明该文件不是一张<span style="color: black;">照片</span>。例如:</p><span style="color: black;">$filename</span>&nbsp;=&nbsp;<span style="color: black;">test.jpg</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p><span style="color: black;">$image_info</span>=&nbsp;getimagesize(<span style="color: black;">$filename</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p><span style="color: black;">if</span>&nbsp;(<span style="color: black;">$image_info</span>&nbsp;!==&nbsp;<span style="color: black;">false</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>&nbsp;<span style="color: black;">"<span style="color: black;">$filename</span>&nbsp;is&nbsp;an&nbsp;image&nbsp;file."</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>}&nbsp;<span style="color: black;">else</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>&nbsp;<span style="color: black;">"<span style="color: black;">$filename</span>&nbsp;is&nbsp;not&nbsp;an&nbsp;image&nbsp;file."</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">需要<span style="color: black;">重视</span>的是,getimagesize函数需要依赖PHP的GD扩展,<span style="color: black;">因此呢</span>在<span style="color: black;">运用</span>之前需要<span style="color: black;">保证</span>该扩展已被启用。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以上介绍了几种常用的判断文件类型的PHP函数,它们各有优劣,<span style="color: black;">能够</span><span style="color: black;">按照</span><span style="color: black;">实质</span><span style="color: black;">状况</span><span style="color: black;">选取</span><span style="color: black;">运用</span>。需要<span style="color: black;">重视</span>的是,在<span style="color: black;">运用</span>这些函数时,<span style="color: black;">因为</span>文件的类型<span style="color: black;">是由于</span>文件标头决定的,<span style="color: black;">因此呢</span>可能会存在某些文件不规范<span style="color: black;">引起</span>判断错误的<span style="color: black;">状况</span>。</p>




4lqedz 发表于 2024-10-5 22:35:05

你说得对,我们一起加油,未来可期。

7wu1wm0 发表于 2024-10-17 05:39:48

论坛的成果是显著的,但我们不能因为成绩而沾沾自喜。
页: [1]
查看完整版本: 判断文件类型的PHP函数