首页 > Web开发 > 详细

关于单文件上传的封装

时间:2016-01-23 18:12:37      阅读:221      评论:0      收藏:0      [点我收藏+]
<?php 
//$fileInfo=$_FILES[‘myFile‘];
/**
* @param $fileInfo string  上传的文件参数 name 
* @param uploadPath string   文件上传的路径,默认的为uploads
* @param allowExt   array   运行文件上传的类型 默认的为:‘jpeg‘,‘jpg‘,‘gif‘,‘png‘
* @param maxSize  int   允许文件上传的最大限制,默认为2兆
*/

function uploadFile($fileInfo,$uploadPath = uploads,$flag=true,$allowExt=array(jpeg,jpg,gif,png),$maxSize = 2097152){
    // 判断错误号
    if ($fileInfo [error] > 0) {
        switch ($fileInfo [error]) {
            case 1 :
                $mes = 上传文件超过了PHP配置文件中upload_max_filesize选项的值;
                break;
            case 2 :
                $mes = 超过了表单MAX_FILE_SIZE限制的大小;
                break;
            case 3 :
                $mes = 文件部分被上传;
                break;
            case 4 :
                $mes = 没有选择上传文件;
                break;
            case 6 :
                $mes = 没有找到临时目录;
                break;
            case 7 :
            case 8 :
                $mes = 系统错误;
                break;
        }
        echo ( $mes );
        return false;
    }
    $ext = pathinfo ( $fileInfo [name], PATHINFO_EXTENSION );
//     $allowExt = array (
//             ‘jpeg‘,
//             ‘jpg‘,
//             ‘png‘,
//             ‘gif‘ 
//     );
    if(!is_array($allowExt)){
        exit(系统错误);
    }
    // 检测上传文件的类型
    if (! in_array ( $ext, $allowExt )) {
        exit ( 非法文件类型 );
    }
    //$maxSize = 2097152; // 2M
                      // 检测上传文件大小是否符合规范
    if ($fileInfo [size] > $maxSize) {
        exit ( 上传文件过大 );
    }
    //检测图片是否为真实的图片类型
    //$flag=true;    
    if($flag){
        if(!getimagesize($fileInfo[tmp_name])){
            exit(不是真实图片类型);
        }
    }
    // 检测文件是否是通过HTTP POST方式上传上来
    if (! is_uploaded_file ( $fileInfo [tmp_name] )) {
        exit ( 文件不是通过HTTP POST方式上传上来的 );
    }
    //$uploadPath = ‘uploads‘;
    if (! file_exists ( $uploadPath )) {
        mkdir ( $uploadPath, 0777, true );
        chmod ( $uploadPath, 0777 );
    }
    $uniName = md5 ( uniqid ( microtime ( true ), true ) ) . . . $ext;
    $destination = $uploadPath . / . $uniName;
    if (! @move_uploaded_file ( $fileInfo [tmp_name], $destination )) {
        exit ( 文件移动失败 );
    }
    
    //echo ‘文件上传成功‘;
//     return array(
//         ‘newName‘=>$destination,
//         ‘size‘=>$fileInfo[‘size‘],
//         ‘type‘=>$fileInfo[‘type‘]
//     );
    return $destination;
}

函数的调用如下doAction.php:

<?php 
header(content-type:text/html;charset=utf-8);
include_once upload.func.php;
$fileInfo=$_FILES[myFile];
// $newName=uploadFile($fileInfo);
// echo $newName;
// $newName=uploadFile($fileInfo,‘imooc‘);
// echo $newName;
//$allowExt=‘txt‘;
$allowExt=array(jpeg,jpg,png,gif,html,txt);
$newName=uploadFile($fileInfo,imooc,false,$allowExt);
echo $newName;

前段代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<form action="doAction3.php" method="post" enctype="multipart/form-data">
<!-- <input type="hidden" name="MAX_FILE_SIZE" value=176942 /> -->
请选择您要上传的文件:<input type="file" name=myFile />
<!-- <input type="file" name="myFile"  accept="image/jpeg,image/gif,image/png"/><br /> -->
<input type="submit" value="上传文件" />
</form>
</body>
</html>

 

关于单文件上传的封装

原文:http://www.cnblogs.com/xs-yqz/p/5153666.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!