本文实例为大家分享了TP3.2批量上传文件或图片的具体代码,并解决了同名冲突问题,供大家参考,具体内容如下
1、html
2、PHP
maxSize = 3145728;
$upload->rootPath = './Uploads/';
$upload->savePath = 'image/';
//$upload->saveName = date('YmdHis').'-'.randomkeys(3);//msectime(),毫秒数13位
$upload->saveName = 'msectime'; //自定义函数,采用13位毫秒和3位随机数
$upload->exts = array('jpg','gif','png','jpeg');
$upload->autoSub = true;
$upload->subName = array('date','Ymd');
/* 判断$_FILES[$key]是否:一维数组,单张<a href="/tag/tupian/" target="_blank" class="keywords">图片</a><a href="/tag/shangchuan/" target="_blank" class="keywords">上传</a> -xzz0703
* 原理:html的input type = "file" name="IDcard"和name="IDcard[]"的区别:
* $_FILES前者到<a href="/tag/houtai/" target="_blank" class="keywords">后台</a><a href="/tag/PHP/" target="_blank" class="keywords">PHP</a>是二维数组,后者是三维数组
*/
foreach($_FILES as $key=>$value){
if(count($_FILES[$key]) == count($_FILES[$key],1)){
$info = $upload->uploadOne($_FILES[$key]);
if($info){
echo json_encode(array('code'=>200,'id'=>$img_id,'name'=>$img_name));exit;
}else{
echo json_encode(array('code'=>0,'msg'=>$upload->getError()));exit;
}
}
}
if(count($_FILES)){
$info = $upload->upload();//如果是二维数组,使用批量<a href="/tag/shangchuan/" target="_blank" class="keywords">上传</a><a href="/tag/wenjian/" target="_blank" class="keywords">文件</a>的<a href="/tag/fangfa/" target="_blank" class="keywords">方法</a>
if(!$info){
$this->error($upload->getError());
exit;
}
$img_url = '/Uploads/'.$info[0]['savepath'].$info[0]['savename'];
$res = array('imgPath1'=>$img_url,code=>$img_url,'msg'=>$info);
echo json_encode($res);
}
}
}
3、核心:
很多朋友在使用TP3.2框架的时候,在saveName属性上面被卡住了,原因就是上传服务器处理级别的百万微秒,很快。解决:saveName = 13位的毫秒+3位随机数,完美解决,具体代码:
PHP;">
//返回当前的毫秒时间戳和随机数合并的字符串
function msectime() {
list($msec,$sec) = explode(' ',microtime());
$msectime = (float)sprintf('%.0f',(floatval($msec) + floatval($sec)) * 1000).randomkeys(3);
return $msectime;
}