php-上传文件函数基础使用

转载自http://www.cnblogs.com/luma/p/7461271.html

前台form表单:

    <form action="upload.class.php" method="post" enctype="multipart/form-data">
        请选择上传文件:<input type="file" name="myFile"><br/>
        <input type="submit" name="上传">
    </form>

php处理:

复制代码
<?php
class upload{
    // $fileName = $_FILES['myFile']['name'];
    // $type = $_FILES['myFile']['type'];
    // $tmp_name = $_FILES['myFile']['tmp_name'];
    // $error = $_FILES['myFile']['error'];
    // $size = $_FILES['myFile']['size'];
    private $fileInfo;  
    function __construct($file){
        $this->fileInfo = $file;
    }
    //$paht存放路径,$allowExt允许上传格式,$maxSize限制文件大小
    public function uploadFile($path="C:/wamp64/www/demo/upload/uploads/",$allowExt=array('gif','jpeg','jpg','png','wbmp'),$maxSize = 52100,$imgFlag = true){
        //判断错误信息
        if($this->fileInfo['error'] == UPLOAD_ERR_OK){
            $ext = $this->getExt($this->fileInfo['name']);
            //限制文件类型
            if(!in_array($ext,$allowExt)){
                exit ($mes = '非法文件类型');
            }
            //限制大小
            if($this->fileInfo['size']>$maxSize){
                exit ($mes = '文件过大');
            }
            //检测类型
            if($imgFlag){
                //getimagesize($fileName)验证是否是图片
                @$info = getimagesize($this->fileInfo['tmp_name']);
                if(!$info){
                    exit (@$mes = "不是真正的图片");
                }
            }

            $this->fileInfo['name'] = $this->getUniName().".".$ext;
            if(!file_exists($path)){
                mkdir($path,0777,true);
            }
            $destination = $path."/".$this->fileInfo['name'];
            //is_uploaded_file($tmp_name)   判断是否通过POST方式上传的
            if(is_uploaded_file($this->fileInfo['tmp_name'])){
                if(move_uploaded_file($this->fileInfo['tmp_name'], $destination)){
                    $mes = "文件上传成功";
                }else{
                    $mes = "文件移动失败";
                }
        }else{
                $mes = "文件不是通过POST方式上传的";
            }
        }else{
            switch ($this->fileInfo['error']) {
                case 1:
                    //  UPLOAD_ERR_INI_SIZE
                    $mes = "超过了配置文件上传文件大小";
                    break;
                case 2:
                    //  UPLOAD_ERR_FORM_SIZE
                    $mes = "超过了表单设置上传文件的大小";
                    break;
                case 3:
                    //  UPLOAD_ERR_PARTIAL
                    $mes = "文件部分被上传";
                    break;
                case 4:
                    //  UPLOAD_ERR_NO_FILE
                    $mes = "没有文件被上传";
                    break;
                case 6:
                    //  UPLOAD_ERR_NO_TMP_DIR
                    $mes = "没有找到临时目录";
                    break;
                case 7:
                    //  UPLOAD_ERR_INI_SIZE
                    $mes = "文件不可写";
                    break;
                case 8:
                    $mes = "由于PHP的扩展程序中断了文件上传";
                    break;
            }
        }
    }
    //取唯一的名称
    public function getUniName(){
        return md5(uniqid(microtime(true),true));
    }
    //后缀
    public function getExt($fileInfo){
        $tmp = explode(".",$this->fileInfo['name']);
        return strtolower(end($tmp));
    }
}
//实例化对象
header("content-type:text/html; charset=utf-8");
$file = $_FILES['myFile'];
$upload = new upload($file);
$upload->uploadFile();
复制代码

上传函数的基本使用,修改封装为一个类去使用。

遇到的问题:类的使用

  1. 类的成员变量为私有时,需要通过函数传递。
  2. 类内方法调用成员变量或其他方法是需要$this引用即可,不需要其他传递。

使用到的函数有:

  1. end() 将 array 的内部指针移动到最后一个单元并返回其值
  2. array explode() 返回由字符串组成的数组
  3. is_uploaded_file — 判断文件是否是通过 HTTP POST 上传的
  4. move_uploaded_file — 将上传的文件移动到新位置
  5. string uniqid 获取一个带前缀、基于当前时间微秒数的唯一ID
  6. mixed microtime 当前 Unix 时间戳以及微秒数

猜你喜欢

转载自blog.csdn.net/Su_Xingyu/article/details/79799160
今日推荐