教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP 中获取文件扩展名的正确方法

PHP 中获取文件扩展名的正确方法

发布时间:2016-10-26   编辑:jiaochengji.com
教程集为您提供PHP 中获取文件扩展名的正确方法等资源,欢迎您收藏本站,我们将为您提供最新的PHP 中获取文件扩展名的正确方法资源
在 PHP 中,如何获取文件扩展名名呢,我们可能会用字符串操作函数进行处理,但下面举的这些方法只是获取了用户的名字后面带.后面的后缀了并不能做安全验证哦。

比如:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8364')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8364>

echo substr(strrchr($file_name,'.'),1);

或者

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9651')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9651>

echo end(explode(".", $filename));

最正确的方法应该是使用 pathinfo 函数,如:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6370')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6370>

echo pathinfo($filename, PATHINFO_EXTENSION);

pathinfo 函数支持4种类型的返回:

PATHINFO_DIRNAME – 目录
PATHINFO_BASENAME – 文件名(含扩展名)
PATHINFO_EXTENSION – 扩展名
PATHINFO_FILENAME – 文件名(不含扩展名)

例子

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8635')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8635>

function get_extension($filename){
return pathinfo($filename,PATHINFO_EXTENSION);
}

调用

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2500')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2500>

<?php
print_r(pathinfo("/testweb/test.txt"));
?>

以上将输出如下结果:

Array(
 [dirname] => /testweb
 [basename] => test.txt
 [extension] => txt
)

如果要上传的用户最好要加以下面方法处理

$extension=$upfile['type']; //这个是由upload上传过来的文件信息

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2053')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2053>

switch( $extension )
   {
    case 'application/msword':
    $extension ='doc';
    break; www.jiaochengji.com
    case 'application/vnd.ms-excel':
    $extension ='xls';
    break;
    case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
    $extension ='docx';
    break;
    case 'application/vnd.ms-powerpoint':
    $extension ='ppt';
    break;
    case 'application/pdf':
    $extension ='pdf';
    break;
    case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
    $extension ='xlsx';
    break;
    default:
    die('只允许上传doc,docx,xls,pdf,ppt文件 <a href="wend.php">重新上传</a>');
   
   }

您可能感兴趣的文章:
php获取文件扩展名的几种方法
php获取文件扩展名的二种方法
php 取得文件后缀(扩展名)的方法
php 文件扩展名获取方法汇总
PHP 获取文件扩展名的方法
php 获取文件扩展名的5种方法
php判断上传文件的文件类型的几种方法
PHP添加CURL扩展库的二种方法
php 从url中获取文件扩展名
php 获取文件扩展名的三个方法

[关闭]
~ ~