php 图片上传 并返回上传文件位置 支持多文件上传

<?php
/**
 * Created by PhpStorm.
 * User: DY040
 * Date: 2018/4/26
 * Time: 13:23
 */
echo '<pre>';

/*
 * 支持多文件上传 检查所有的文件 是否符合要求 储存于 服务器 并返回图片地址
 * */

class ImgUpload1
{
    protected static $imgs_file = array();//要返回的数据 (储存的文件信息)

    function __construct($files)
    {

        foreach ($files as $k => $v) {
            $img_info = $files[$k];
            $arr = array();
            if (gettype($img_info['name']) === 'array') {

                foreach ($img_info['name'] as $key => $value) {
                    $arr['name'] = $img_info['name'][$key];
                    $arr['type'] = $img_info['type'][$key];
                    $arr['tmp_name'] = $img_info['tmp_name'][$key];
                    $arr['error'] = $img_info['error'][$key];
                    $arr['size'] = $img_info['size'][$key];
                    $this->_check($arr, $k);
                }
            } else {
                $this->_check($img_info, $k);
            }

        }


    }

    protected function _check($img_info, $index)
    {

        if (!isset(static::$imgs_file[$index])) {

            static::$imgs_file[$index] = $this->_img_store($img_info) . "*";
        } else {
            static::$imgs_file[$index] .= $this->_img_store($img_info) . "*";
        }


    }

    protected function _img_store($img_info)
    {

        //检测文件是否合法 ---文件名后缀
        $img_hzm = array('.gif', '.png', '.jpg', '.jpeg');
        $re = in_array(strrchr($img_info['name'], '.'), $img_hzm);
        if (!$re) {
            return '该图片不合法-文件名后缀';
        }
        //检测文件是否合法 ---MIME php检测上传信息的mime
        if (explode('/', $img_info['type'])[0] !== 'image') {
            return '该图片不合法-MIME';
        }
        //检测文件是否合法 ---MIME php检测本地的临时文件
        //        这里要修改php.ini 增加php_fileinfo.dll模块
        $file_info = new Finfo(FILEINFO_MIME_TYPE);
        $mime_type = $file_info->file($img_info['tmp_name']);
        if (explode('/', $mime_type)[0] !== 'image') {
            return '该图片不合法-MIME';
        }
        //检测文件大小 设为1mb
        if ($img_info['size'] > 1000000) {
            return '该图片不合法-文件大于1000kb';
        }
        //是否是http上传的临时文件
        if (!is_uploaded_file($img_info['tmp_name'])) {
            echo '文件名不合法';
            return false;
        };

        //设置上传文件地址
        if (!is_dir('.\upload')) {
            mkdir('.\upload');
        }

        $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'upload\\' . date('Ymdh');//按小时划分目录
        $imgType = strrchr($img_info['name'], '.');//文件名后缀

        $re = is_dir($path);
        if (!$re) {
            mkdir($path);
        }


        $imgName = uniqid(rand(), true) . $imgType;
        $img_file = $path . DIRECTORY_SEPARATOR . $imgName;


        $upload_re = move_uploaded_file($img_info['tmp_name'], $img_file);
        if ($upload_re) {

            return $img_file;


        } else {

            return '文件上传失败';

        }

    }

    public function imgs_file()
    {
        $arr = static::$imgs_file;
        foreach ($arr as $k => $v) {


            if(gettype($_FILES[$k]['name'])==='string'){
                $arr[$k] = substr($v, 0, -1);
            }else{
                $arr[$k] = explode('*', substr($v, 0, -1));
            }
        }
        return $arr;
    }


}

$img_upload = new ImgUpload1($_FILES);
//获取文件所在位置
print_r($img_upload->imgs_file());
/*
 *
 * Array
(
    [user_img] => D:\PHP\www\hxq\day22\upload\2018042604\183775ae1878615f2e6.81933117.jpg
    [pro_img] => Array
        (
            [0] => D:\PHP\www\hxq\day22\upload\2018042604\324625ae18786172b65.94418381.jpg
            [1] => D:\PHP\www\hxq\day22\upload\2018042604\291675ae187861863e5.14296700.jpg
        )

)
 *
 * */
/*
 * 对应表单
 *
 * <form action="imgUpload.class.php" method="post" enctype="multipart/form-data">
    <p><input type="file" name="user_img"></p>

    <p><input type="file" name="pro_img[]"></p>

    <p><input type="file" name="pro_img[]"></p>

    <p><input type="submit"></p>

</form>
 * */

猜你喜欢

转载自www.cnblogs.com/aqigogogo/p/8953180.html