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

    php 上传文件


    html表单,需要记住form的enctype属性:

    <form action='index.php' method="POST" enctype="multipart/form-data">
    <input type='file' name='file' />
    <input type='submit' value='提交' />
    </form>

    php代码:

    function uploadFile($fileInfo,$path='./upload/'){
        //判断错误号
        if($fileInfo['error'] == UPLOAD_ERR_OK){
            //文件是否是通过HTTP POST上传的
            if(is_uploaded_file($fileInfo['tmp_name'])){
                $ext = strtolower(pathinfo($fileInfo['name'],PATHINFO_EXTENSION));
                $allow_Ext = array('jpg','jpeg','png');
                if(in_array($ext, $allow_Ext)){
                    $length = 6;
                    $newFileName = substr(md5(uniqid(microtime(true),true)),0,$length);
                    $savePath = $path . $newFileName .'.'.$ext;
                    if(move_uploaded_file($fileInfo['tmp_name'], $savePath)){
                        return 'success';
                    }else{
                        return 'file upload fail';
                    }
                }else{
                    return 'file type not allow';
                }
            }else{
                return '文件不是通过HTTP POST上传的';
            }
        }else{
            switch($fileInfo['error']){
                case 1:
                    $errMsg = '超出了配置文件的大小';//PHP.ini的upload_max_filesize的值
                    break;
                case 2:
                    $errMsg = '超过了表单允许接收数据的大小';//表单中MAX_FILE_SIZE的值
                    break;
                case 3:
                    $errMsg = '文件部分被上传';
                    break;
                case 4:
                    $errMsg = '没有文件被上传';
                    break;
                case 6:
                    $errMsg = '找不到临时文件夹';
                    break;
                case 7:
                    $errMsg = '文件写入失败';
                    break;
                default: 
                    $errMsg = "Unknown upload error"; 
                    break;     
            }
            return $errMsg;
        }
    }
    //var_dump($_FILES);
    if(isset($_FILES['file'])){
       echo uploadFile($_FILES['file']);
    }

  • 相关阅读:
    正则表达式
    浅谈xss攻击
    四舍五入[银行家算法]
    POJ-2442-Sequence(二叉堆)
    Spring MVC 启动报错
    WebMagic 抓取图片并保存至本地
    spring 定时任务
    jquery validate 自定义校验方法
    位图
    二叉树(线索化)
  • 原文地址:https://www.cnblogs.com/gyfluck/p/9952424.html
Copyright © 2011-2022 走看看