判断文件类型的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 mime_content_type ( string <span style="color: black;">$filename</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>参数,返回该文件的MIME类型。例如:</p><span style="color: black;">$filename</span> = <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> = 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> <span style="color: black;">"The MIME type of <span style="color: black;">$filename</span> is: <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 finfo_file ( resource <span style="color: black;">$finfo</span> , string <span style="color: black;">$filename</span> [, int <span style="color: black;">$options</span> = FILEINFO_NONE [, resource <span style="color: black;">$context</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;">其中,$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;"> = finfo_open(FILEINFO_MIME_TYPE);</p><span style="color: black;">$filename</span> = <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> = finfo_file(<span style="color: black;">$finfo</span>, <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> <span style="color: black;">"The MIME type of<span style="color: black;">$filename</span> is: <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 pathinfo ( string <span style="color: black;">$path</span> [, int <span style="color: black;">$options</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">= PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] )</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> = <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> = 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> <span style="color: black;">"The extension of<span style="color: black;">$filename</span> is: "</span> . <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> getimagesize ( string <span style="color: black;">$filename</span> [, array &<span style="color: black;">$imageinfo</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;">其中,$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> = <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>= 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> (<span style="color: black;">$image_info</span> !== <span style="color: black;">false</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> <span style="color: black;">echo</span> <span style="color: black;">"<span style="color: black;">$filename</span> is an image file."</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>} <span style="color: black;">else</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p> <span style="color: black;">echo</span> <span style="color: black;">"<span style="color: black;">$filename</span> is not an image 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>
你说得对,我们一起加油,未来可期。 论坛的成果是显著的,但我们不能因为成绩而沾沾自喜。
页:
[1]