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>
 * */

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM