热搜:NVER node 开发 php

thinkphp3.2 uploadify上传 guid在一次上传中只调用一次 如何实现?

2024-08-17 14:00:02
thinkphp3.2 uploadify上传 guid在一次上传中只调用一次 如何实现?

我想实现的是uploadify上传图片 写入数据库里 自动生成个token 就想到了guid 

上传类用的就是thinkphp本身的 Upload类  结果插入到数据库发现 每个图片都不一样
如下图这样



uploadyify上传后是这样处理的

  public function  uploadify(){        if(!empty($_FILES)){            $config =array(                'maxSize' => 1024000,// 附件大小                'savePath'=> "attachment/picture/uploadify/",//附件保存路径                'saveName'=>array('uniqid',''),//附件保存名称                'exts'=>array('jpg','jpeg','png','gif','rar','zip'),//附件类型                'autoSub'=>true,//开启子目录保存                'subName'=>array('date','Ymd')//子目录命名规则            );            $upload=new \Think\Upload($config);            $files_upload=$upload->upload();            if($files_upload){                foreach($files_upload as $file){//                 $info= $files_upload['Filedate']['savepath'].$files_upload['Filedata']['savename'];//                 上传后文件名                    $savename=$file['savename'];//                 上传前文件名                    $name=$file['name'];//                 上传文件类型                    $type=$file['ext'];//                 上传文件大小 k                    $size=$file['size'];//                 上传目录                    $path=$file['savepath'].$file['subname'].$file['savename'];                                      $attachment=D('picture_attachment');                    $date['creattime']=date('Y-m-d H:i:s',time());                    $date['pictureurl']=$path;                    $date['oldname']=$name;                    $date['newname']=$savename;                    $date['picturesize']=$size;                    $date['picturetype']=$type;                    $date['pictureid'] =$this->guid();// token guid                   if( $attachment->create($date)){                        $attachment->add();                        session('guid',$date['pictureid']);                   /*   返回给前台JS调用*/                       echo $path ;                   }                    else{                        unlink($path.$type);                        session("FilesInFo",null);                    }                } ;            }else{                /*返回报错信息*/                $info=$this->error($upload->getError(),U('picture/add'),30);                echo $info;            }        }        else{            echo '上传文件为空';        }// end    }


这样就是遍历上传的文件时候 都生成一次调用 $date['pictureid'] =$this->guid() 这个如何只执行一次
这样guid多图的 token就一样了~ 求大神指点 具体怎么做啊 麻烦熟悉TP3.2的 大神帮忙了 ~谢谢


回复讨论(解决方案)

是指这个吗?$date['pictureid'] =$this->guid();// token guid
你在循环里执行,自然每次不一样
你把它放到循环前面去,不就只执行一次了吗?

是指这个吗?$date['pictureid'] =$this->guid();// token guid
你在循环里执行,自然每次不一样
你把它放到循环前面去,不就只执行一次了吗?



这样也是不行的 是不是本身使用的upload类的问题呢 ?

这个类超过1W字符的限制了。。。。。
熟悉TP的大神 如果看到 是不是这里的处理方式的问题呢 放到循环外面不行的啊
ThinkPHP\Library\Think\Upload.class.php


是指这个吗?$date['pictureid'] =$this->guid();// token guid
你在循环里执行,自然每次不一样
你把它放到循环前面去,不就只执行一次了吗?



这样也是不行的 是不是本身使用的upload类的问题呢 ?

这个类超过1W字符的限制了。。。。。
熟悉TP的大神 如果看到 是不是这里的处理方式的问题呢 放到循环外面不行的啊
ThinkPHP\Library\Think\Upload.class.php


在放个
$upload=new \Think\Upload($config);
$files_upload=$upload->upload();
这两个在upload类里执行的方法
<?phpnamespace Think;class Upload {   /**     * 默认上传配置     * @var array     */    private $config = array(        'mimes'         =>  array(), //允许上传的文件MiMe类型        'maxSize'       =>  0, //上传的文件大小限制 (0-不做限制)        'exts'          =>  array(), //允许上传的文件后缀        'autoSub'       =>  true, //自动子目录保存文件        'subName'       =>  array('date', 'Y-m-d'), //子目录创建方式,[0]-函数名,[1]-参数,多个参数使用数组        'rootPath'      =>  './', //保存根路径        'savePath'      =>  '', //保存路径        'saveName'      =>  array('uniqid', ''), //上传文件命名规则,[0]-函数名,[1]-参数,多个参数使用数组        'saveExt'       =>  '', //文件保存后缀,空则使用原后缀        'replace'       =>  true, //存在同名是否覆盖        'hash'          =>  true, //是否生成hash编码        'callback'      =>  false, //检测文件是否存在回调,如果存在返回文件信息数组        'driver'        =>  '', // 文件上传驱动        'driverConfig'  =>  array(), // 上传驱动配置    );    /**     * 上传错误信息     * @var string     */    private $error = ''; //上传错误信息    /**     * 上传驱动实例     * @var Object     */    private $uploader;    /**     * 构造方法,用于构造上传实例     * @param array  $config 配置     * @param string $driver 要使用的上传驱动 LOCAL-本地上传驱动,FTP-FTP上传驱动     */    public function __construct($config = array(), $driver = '', $driverConfig = null){        /* 获取配置 */        $this->config   =   array_merge($this->config, $config);        /* 设置上传驱动 */        $this->setDriver($driver, $driverConfig);        /* 调整配置,把字符串配置参数转换为数组 */        if(!empty($this->config['mimes'])){            if(is_string($this->mimes)) {                $this->config['mimes'] = explode(',', $this->mimes);            }            $this->config['mimes'] = array_map('strtolower', $this->mimes);        }        if(!empty($this->config['exts'])){            if (is_string($this->exts)){                $this->config['exts'] = explode(',', $this->exts);            }            $this->config['exts'] = array_map('strtolower', $this->exts);        }    }  /**     * 上传文件     * @param 文件信息数组 $files ,通常是 $_FILES数组     */    public function upload($files='') {        if('' === $files){            $files  =   $_FILES;        }        if(empty($files)){            $this->error = '没有上传的文件!';            return false;        }        /* 检测上传根目录 */        if(!$this->uploader->checkRootPath($this->rootPath)){            $this->error = $this->uploader->getError();            return false;        }        /* 检查上传目录 */        if(!$this->uploader->checkSavePath($this->savePath)){            $this->error = $this->uploader->getError();            return false;        }        /* 逐个检测并上传文件 */        $info    =  array();        if(function_exists('finfo_open')){            $finfo   =  finfo_open ( FILEINFO_MIME_TYPE );        }        // 对上传文件数组信息处理        $files   =  $this->dealFiles($files);            foreach ($files as $key => $file) {            if(!isset($file['key']))   $file['key']    =   $key;            /* 通过扩展获取文件类型,可解决FLASH上传$FILES数组返回文件类型错误的问题 */            if(isset($finfo)){                $file['type']   =   finfo_file ( $finfo ,  $file['tmp_name'] );            }            /* 获取上传文件后缀,允许上传无后缀文件 */            $file['ext']    =   pathinfo($file['name'], PATHINFO_EXTENSION);            /* 文件上传检测 */            if (!$this->check($file)){                continue;            }            /* 获取文件hash */            if($this->hash){                $file['md5']  = md5_file($file['tmp_name']);                $file['sha1'] = sha1_file($file['tmp_name']);            }            /* 调用回调函数检测文件是否存在 */            $data = call_user_func($this->callback, $file);            if( $this->callback && $data ){                if ( file_exists('.'.$data['path'])  ) {                    $info[$key] = $data;                    continue;                }elseif($this->removeTrash){                    call_user_func($this->removeTrash,$data);//删除垃圾据                }            }            /* 生成保存文件名 */            $savename = $this->getSaveName($file);            if(false == $savename){                continue;            } else {                $file['savename'] = $savename;            }            /* 检测并创建子目录 */            $subpath = $this->getSubPath($file['name']);            if(false === $subpath){                continue;            } else {                $file['savepath'] = $this->savePath . $subpath;            }            /* 对图像文件进行严格检测 */            $ext = strtolower($file['ext']);            if(in_array($ext, array('gif','jpg','jpeg','bmp','png','swf'))) {                $imginfo = getimagesize($file['tmp_name']);                if(empty($imginfo) || ($ext == 'gif' && empty($imginfo['bits']))){                    $this->error = '非法图像文件!';                    continue;                }            }            /* 保存文件 并记录保存成功的文件 */            if ($this->uploader->save($file,$this->replace)) {                unset($file['error'], $file['tmp_name']);                $info[$key] = $file;            } else {                $this->error = $this->uploader->getError();            }        }        if(isset($finfo)){            finfo_close($finfo);        }        return empty($info) ? false : $info;    }   /**     * 转换上传文件数组变量为正确的方式     * @access private     * @param array $files  上传的文件变量     * @return array     */    private function dealFiles($files) {        $fileArray  = array();        $n          = 0;        foreach ($files as $key=>$file){            if(is_array($file['name'])) {                $keys       =   array_keys($file);                $count      =   count($file['name']);                for ($i=0; $i<$count; $i++) {                    $fileArray[$n]['key'] = $key;                    foreach ($keys as $_key){                        $fileArray[$n][$_key] = $file[$_key][$i];                    }                    $n++;                }            }else{               $fileArray = $files;               break;            }        }       return $fileArray;    }

是不是在 这里的问题呢?
    /* 逐个检测并上传文件 */
        $info    =  array();
        if(function_exists('finfo_open')){
            $finfo   =  finfo_open ( FILEINFO_MIME_TYPE );
        }
        // 对上传文件数组信息处理
        $files   =  $this->dealFiles($files);    
        foreach ($files as $key => $file) {
            if(!isset($file['key']))   $file['key']    =   $key;
            /* 通过扩展获取文件类型,可解决FLASH上传$FILES数组返回文件类型错误的问题 */
            if(isset($finfo)){
                $file['type']   =   finfo_file ( $finfo ,  $file['tmp_name'] );
            }