TP3.2批量上傳文件或圖片 同名沖突問題的解決方法
本文實例為大家分享了TP3.2批量上傳文件或圖片的具體代碼,并解決了同名沖突問題,供大家參考,具體內(nèi)容如下
1、html
<form action="{:U('Upload/index')}" enctype="multipart/form-data" method="post" >
<p><input type="file" id="file3" name="ID[]" /></p>
<p><input type="file" id="file4" name="ID[]" /></p>
<input type="submit" value="上傳" />
<p><img id="img1" alt="" src="/Public/IMAGE/empty_thumb.gif" /></p>
</form>
2、php
public function index(){
if(!empty($_FILES)){
$upload = new \Think\Upload();// 實例化上傳類
$upload->maxSize = 3145728;
$upload->rootPath = './Uploads/';
$upload->savePath = 'image/';
//$upload->saveName = date('YmdHis').'-'.randomkeys(3);//msectime(),毫秒數(shù)13位
$upload->saveName = 'msectime'; //自定義函數(shù),采用13位毫秒和3位隨機數(shù)
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');
$upload->autoSub = true;
$upload->subName = array('date','Ymd');
/* 判斷$_FILES[$key]是否:一維數(shù)組,單張圖片上傳 -xzz0703
* 原理:html的input type = "file" name="IDcard"和name="IDcard[]"的區(qū)別:
* $_FILES前者到后臺php是二維數(shù)組,后者是三維數(shù)組
*/
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();//如果是二維數(shù)組,使用批量上傳文件的方法
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屬性上面被卡住了,原因就是上傳服務(wù)器處理級別的百萬微秒,很快。
解決:saveName = 13位的毫秒+3位隨機數(shù),完美解決,具體代碼:
//返回當(dāng)前的毫秒時間戳和隨機數(shù)合并的字符串
function msectime() {
list($msec, $sec) = explode(' ', microtime());
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000).randomkeys(3);
return $msectime;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
TP5(thinkPHP5)框架基于ajax與后臺數(shù)據(jù)交互操作簡單示例
這篇文章主要介紹了TP5(thinkPHP5)框架基于ajax與后臺數(shù)據(jù)交互操作,結(jié)合實例形式分析了thinkPHP5前端基于jQuery的ajax數(shù)據(jù)提交及后臺數(shù)據(jù)接收、處理相關(guān)操作技巧,需要的朋友可以參考下2018-09-09
Laravel6.0.4中將添加計劃任務(wù)事件的方法步驟
此版本包括計劃任務(wù)事件、新的 JSON 斷言方法和所有最新更改。這篇文章主要介紹了Laravel6.0.4中將添加計劃任務(wù)事件的方法步驟,感興趣的可以了解一下2019-10-10
ThinkPHP 6 添加跳轉(zhuǎn)提示擴展 liliuwei/thinkphp-jump的操作
liliuwei/thinkphp-jump 是 TP5 中經(jīng)典跳轉(zhuǎn)提示,在 TP6 中已經(jīng)取消,通過 composer 下載該擴展可以在 TP6 中使用 TP5 的跳轉(zhuǎn)提示操作,這篇文章主要介紹了ThinkPHP 6 添加跳轉(zhuǎn)提示擴展 liliuwei/thinkphp-jump,需要的朋友可以參考下2023-08-08
laravel數(shù)據(jù)庫查詢結(jié)果自動轉(zhuǎn)數(shù)組修改實例
這篇文章主要介紹了laravel數(shù)據(jù)庫查詢結(jié)果自動轉(zhuǎn)數(shù)組修改實例,有需要的同學(xué)可以借鑒參考下2021-02-02

