晓夏

北漂的女孩

Good Luck To You!

php CI框架同时上传2张不同大小和不同尺寸的图片

浏览量:427

控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller
{
    /**
     * author        : lianghuiju@jiayuan.com
     * function_name : index
     * description   :
     */
    public function index() {
        if ($_FILES['imgfile']['name'] != '') {//详情主图
            $imgfile = $this->upload("100","400","400","imgfile");
            echo "imgfile:";
            var_dump($imgfile);
        }
        echo "<br/>";
        if ($_FILES['litterimg']['name'] != '') {//矩形图
            $little_img = $this->upload("150","750","350","litterimg");
            echo "ittle_img:";
            var_dump($little_img);
        }
    }
    /**
     * 图片上传
     * @param $size  int 图片的大小
     * @param $width int  图片的宽度
     * @param $height int 图片的高度
     * @param $name  string  form表单name名字
     * @param $randname string   上传图片的name
     * @return mixed
     * author        : lianghuiju@jiayuan.com
     * function_name : upload
     * description   :
     */
    function upload($size, $width, $height, $name,$randname) {
        $config['upload_path'] = './uploads/';//路径
        $config['allowed_types'] = 'gif|jpg|png|jpeg';//类型
        $config['max_size'] = $size;//100k以内
        $config['max_width'] = $width;
        $config['max_height'] = $height;
        $config['file_name'] = $this->getNewName($randname); //文件名不使用原始名
        $this->load->library('upload');
        $this->upload->initialize($config);
        if (!$this->upload->do_upload($name)) {
            return $this->upload->display_errors();
        } else {
            $data['upload_data'] = $this->upload->data();  //文件的一些信息
            $img = $data['upload_data']['file_name'];  //取得文件名
            return $img;
        }
    }

    /**
     * 生成一个无重复的文件名
     * @param $filename string   上传图片的name
     * @return string  上传图片的文件名
     * author        : lianghuiju@jiayuan.com
     * function_name : getNewName
     * description   :
     */
    function getNewName($filename) {
        //年月日时分秒格式的字符串
        $timeNow = date('YmdHis', time());
        //生成一个8位小写字母的随机字符串
        $randKey = '';
        for ($a = 0; $a < 8; $a++) {
            $randKey .= chr(mt_rand(97, 122));
        }
        $fileParts = pathinfo($filename);//取得文件关联数组信息
        $name =  strtolower($fileParts['extension']);//文件扩展名转换为小写
        $extName = "." . $name;//取得原文件的扩展名
        //组成新文件名
        $newName = $timeNow . $randKey . $extName;
        return $newName;
    }

}

html文件:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Welcome to CodeIgniter</title>
<body>
<form action="index.php?c=Welcome&m=index"  name="theForm" method="post" enctype="multipart/form-data">
    商品图片:<input type="file" name="imgfile"/>图片大小:(400*400)<br/><br/>
    拼团矩形图:<input type="file" name="litterimg"/>图片大小:(750*350)<br/><br/>
    <input type="submit" value="提交"/>
</form>
</body>
</html>


神回复

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。