Thinkphp3.2.3 多圖上傳,同類型只能上傳一張問題
阿新 • • 發佈:2018-11-26
Thinkphp3.2.3 多圖上傳,同類型只能上傳一張問題,
例如一次性上傳多張 .jpg 檔案,只能上傳最後一張,
問題在於,同名,只儲存一張,找了很多方法,問題就在於 saveName 上
在 App->Common->Common->function.php 下加入
function guid() { if (function_exists('com_create_guid')) { return com_create_guid(); } else { mt_srand((double) microtime() * 10000); $charid = strtoupper(md5(uniqid(rand(), true))); $hyphen = chr(45); $uuid = chr(123) . substr($charid, 0, 8) . $hyphen . substr($charid, 8, 4) . $hyphen . substr($charid, 12, 4) . $hyphen . substr($charid, 16, 4) . $hyphen . substr($charid, 20, 12) . chr(125); return $uuid; } }
再在 upload方法中 saveName = array(‘guid’, ‘’);
完整程式碼如下
public function upload() { $upload = new \Think\Upload(); $upload->maxSize = 0; $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); $upload->rootPath = './Public/upload/'; $upload->savePath = 'thumb/'; // $now = $_SERVER['REQUEST_TIME']; $upload->saveName = array('guid', ''); $info = $upload->upload(); if (!$info) { $this->error($upload->getError()); } else { $this->success('上傳成功!'); } }
前端程式碼
<form enctype="multipart/form-data" action="__URL__/upload" method="post">
<input type="file" name="myFile[]" multiple />
<input type="submit" value="提交">
</form>
上面出來的圖片路徑是 {5141258E-8008-E064-26EB-4EC6642B71AB} 這樣的,我們優化一下,把左右的 大括號去掉,並把中劃線改成下劃線。
if (function_exists('com_create_guid')) { return com_create_guid(); } else { mt_srand((double) microtime() * 10000); $charid = strtoupper(md5(uniqid(rand(), true))); $hyphen = chr(95); //下劃線 $uuid = substr($charid, 0, 8) . $hyphen . substr($charid, 8, 4) . $hyphen . substr($charid, 12, 4) . $hyphen . substr($charid, 16, 4) . $hyphen . substr($charid, 20, 12); return $uuid; }
這樣就變成 C4D414B3_D751_173B_BCD9_F8C8CE95C5F5.jpg