zoukankan      html  css  js  c++  java
  • php封装文件上传

    这是一个经常在项目中遇到的问题,所以封装一个,分享给大家。

    一,前期配置php.ini     如果上传文件超过了php配置那么$_POST或者$_FILES等都是空数组,这点是一个坑,因为那时候就不能用$_FILES["uploadfile"]["size"]获取到文件大小了。

    1. upload_max_filesize = 8M    //上传文件大小
    2. post_max_size = 10M   //以POST上传文件大小
    3. memory_limit = 20M  //php脚本运行最大占有内存大小

    二,表单实列a.php

    <form action="b.php" method="POST" enctype="multipart/form-data" >
    <input type="hidden" name="MAX_FILE_SIZE" value="30">

    <!--如果设置上面这行表示,表单上传文件大小的限制,这样$_FILES["upfile"]["size"]为0,$_FILES["upfile"]["error"]为2,后面会有介绍"error"-->

    <input type="file" name="upfile" id="upfile" /><br />
    <br />
    <input type="submit" />
    </form>

    三,php文件b.php

    <?php

    //获得文件扩展名
    function get_file_extention( $fileName ){
    return end(explode(".",$fileName));
    }

    //获得唯一随机名字
    function get_uniname(){
    return md5( uniqid(microtime(true) , true) );
    }

    //上传处理
    function uploadfile($fileInfo,$path="uploads",$allowExt=array("png","jpg","jpeg","gif","zip"),$fileMaxSize=104857600000,$imageTag=true){
    if(!$fileInfo){
    exit("文件信息错误");//可能是文件超出了服务器限制
    };
    if($fileInfo["error"]==0){
    //检查文件大小
    if( $fileInfo['size'] > $fileMaxSize){
    exit("文件大小超过");
    }
    //检查扩展
    $ext = get_file_extention($fileInfo['name']);
    if( !in_array($ext, $allowExt ) ){
    exit("非法文件名");
    }
    //检查是否是真正的图片文件
    if(!$imageTag){
    if( !getimagesize( $fileInfo['tmp_name'])){
    exit("不是真正的图片文件");
    }
    }
    //检查文件是否是上传的文件
    if( !is_uploaded_file( $fileInfo['tmp_name'])){
    exit("非上传文件");
    }
    //检查上传目录是否存在
    if(!file_exists($path))
    {
    mkdir($path,0777,true);
    }
    $fileName = get_uniname().".".$ext;
    $destinationPath = $path."/".$fileName;
    //上传
    if(move_uploaded_file($fileInfo['tmp_name'],$destinationPath)){
    $result =array("result"=>"图片上传成功","code"=>"0000","data"=>$destinationPath);
    }else{
    $result = array("result"=>"图片上传失败","code"=>"1111","data"=>$destinationPath);
    };


    }else{
    switch($fileInfo['error']){
    case 1:
    $result=array("result"=>"超过了配置文件上传文件的大小","code"=>"0001","data"=>"");//UPLOAD_ERR_INI_SIZE
    break;
    case 2:
    $result=array("result"=>"超过了表单设置上传文件的大小","code"=>"0002","data"=>""); //UPLOAD_ERR_FORM_SIZE
    break;
    case 3:
    $result=array("result"=>"文件部分被上传","code"=>"0003","data"=>"");//UPLOAD_ERR_PARTIAL
    break;
    case 4:
    $result= array("result"=>"没有文件被上传","code"=>"0004","data"=>"");//UPLOAD_ERR_NO_FILE
    break;
    case 6:
    $result= array("result"=>"没有找到临时目录","code"=>"0005","data"=>"");//UPLOAD_ERR_NO_TMP_DIR
    break;
    case 7:
    $result= array("result"=>"文件不可写","code"=>"0006","data"=>"");//UPLOAD_ERR_CANT_WRITE;
    break;
    case 8:
    $result= array("result"=>"由于PHP的扩展程序中断了文件上传","code"=>"0006","data"=>"");//UPLOAD_ERR_EXTENSION
    break;
    }
    }

    return $result;
    }
    ?>

  • 相关阅读:
    Item2:建造者替代多参数构造器
    Java常量赋值失败?
    0828 列表 增删改查
    字符 列表的切片规则
    0820 字符转换为数字
    使用 in 判断是否有敏感词
    while循环
    for循环
    isalnum 判断变量是否由字符或者数字组成
    使用lower upper等字符大小写指令选择为大小写单词转换大小写
  • 原文地址:https://www.cnblogs.com/access520/p/5033137.html
Copyright © 2011-2022 走看看