1上传多张图片, 要对 $_FILES进行 重新处理.
1 //添加 2 public function addCourseAlbumAction() 3 { 4 $CourseAlbumModel = new CourseAlbumModel(); 5 $CourseAlbumModel->title = $_REQUEST["title"]; 6 $CourseAlbumModel->courseId = $_REQUEST["courseId"]; 7 if(!empty($_FILES)) 8 { 9 10 $tempArr = $_FILES["url"]; 11 $imageArr = array(); 12 13 foreach ($tempArr as $k => $v) 14 { 15 foreach ($v as $k2 => $v2) 16 { 17 $imageArr[$k2][$k] = $v2; 18 } 19 } 20 21 foreach ($imageArr as $k => $v) 22 { 23 $upload = new BaseUploadUtil(); 24 $upload->createPath(); 25 $upload->createDatePath(); 26 $upload->file = $v; 27 $info = $upload->upload(); 28 if(count($info)>0){ 29 $CourseAlbumModel->url = $info["path"]; 30 } 31 $CourseAlbumModel->orderBy = $_REQUEST["orderBy"]; 32 $CourseAlbumModel->lastUpdateTime = time(); 33 $CourseAlbumModel->insert(); 34 } 35 36 echo 1; 37 38 } 39 else 40 { 41 $CourseAlbumModel->orderBy = $_REQUEST["orderBy"]; 42 $CourseAlbumModel->lastUpdateTime = time(); 43 echo $CourseAlbumModel->insert(); 44 } 45 46 }
关键代码:
1 $tempArr = $_FILES["url"]; 2 $imageArr = array(); 3 4 foreach ($tempArr as $k => $v) 5 { 6 foreach ($v as $k2 => $v2) 7 { 8 $imageArr[$k2][$k] = $v2; 9 } 10 } 11 12 foreach ($imageArr as $k => $v) 13 { 14 $upload = new BaseUploadUtil(); 15 $upload->createPath(); 16 $upload->createDatePath(); 17 $upload->file = $v; 18 $info = $upload->upload(); 19 20 }
处理后的 数组是 $imageArr. 之后 每次上传 就是 $upload->file = $v;
2fileinput 上传多张图片.
// echo BaseView::getImageHtml(array("name"=> "original","label"=> $_courseAlbum->getFieldDesc("original"))); // echo BaseView::getHrHtml(); echo '<fieldset> <div class="form-group"> <label class="col-sm-2 control-label">' . $_courseAlbum->getFieldDesc("original") . ':</label> <div class="col-sm-9"> <input id="original_0" name="original[]" type="file" class="file" multiple="true" /> </div> <script type="text/javascript"> $("#original_0").fileinput({ language: "zh", showUpload:false, browseLabel:"<span style=\'color:#fff;\'>选择'. $_courseAlbum->getFieldDesc("original").'</span>", showClose:false, maxFileCount: 10 }); </script> </div> </fieldset>'; echo BaseView::getHrHtml();
1. name="original[]" 这是一个数组.
2.multiple="true" 允许多选.
3. maxFileCount: 10 最大允许10个文件.
php端代码:
//添加 ---> 上传多张: public function addCourseAlbumAction() { $CourseAlbumModel = new CourseAlbumModel(); $CourseAlbumModel->title = $_REQUEST["title"]; $CourseAlbumModel->courseId = $_REQUEST["courseId"]; // if(!empty($_FILES)){ // $upload = new BaseUploadUtil(); // $upload->createPath(); // $upload->createDatePath(); // $upload->file = $_FILES["original"]; // $info = $upload->upload(); // if(count($info)>0){ // $CourseAlbumModel->original = $info["path"]; // } // } // $CourseAlbumModel->orderBy = $_REQUEST["orderBy"]; // $CourseAlbumModel->lastUpdateTime = time(); // echo $CourseAlbumModel->insert(); if(!empty($_FILES)) { $tempArr = $_FILES["original"]; $imageArr = array(); foreach ($tempArr as $k => $v) { foreach ($v as $k2 => $v2) { $imageArr[$k2][$k] = $v2; } } foreach ($imageArr as $k => $v) { $upload = new BaseUploadUtil(); $upload->createPath(); $upload->createDatePath(); $upload->file = $v; $info = $upload->upload(); if(count($info)>0){ $CourseAlbumModel->original = $info["path"]; } $CourseAlbumModel->orderBy = $_REQUEST["orderBy"]; $CourseAlbumModel->lastUpdateTime = time(); $CourseAlbumModel->insert(); } echo 1; } else { $CourseAlbumModel->orderBy = $_REQUEST["orderBy"]; $CourseAlbumModel->lastUpdateTime = time(); echo $CourseAlbumModel->insert(); } }
3修改,删除的时候删除原来的资源,图片 update, delete
1 //修改 2 public function updateCourseAction() 3 { 4 $CourseModel = new CourseModel($_REQUEST["id"]); 5 $CourseModel->title = $_REQUEST["title"]; 6 $CourseModel->userId = $_REQUEST["userId"]; 7 if(!empty($_FILES['defaultImg'])){ 8 $upload = new BaseUploadUtil(); 9 $upload->createPath(); 10 $upload->createDatePath(); 11 $upload->file = $_FILES["defaultImg"]; 12 $info = $upload->upload(); 13 14 //删除: 15 $this->deleteService($CourseModel->defaultImg); 16 17 if(count($info)>0){ 18 $CourseModel->defaultImg = $info["path"]; 19 } 20 } 21 if(!empty($_FILES['icon'])){ 22 $upload = new BaseUploadUtil(); 23 $upload->createPath(); 24 $upload->createDatePath(); 25 $upload->file = $_FILES["icon"]; 26 $info = $upload->upload(); 27 28 //删除: 29 $this->deleteService($CourseModel->icon); 30 31 if(count($info)>0){ 32 $CourseModel->icon = $info["path"]; 33 } 34 } 35 $CourseModel->remark = isset($_REQUEST["remark"]) ? $_REQUEST["remark"] : ""; 36 $CourseModel->orderBy = $_REQUEST["orderBy"]; 37 $CourseModel->numb = $_REQUEST["numb"]; 38 $CourseModel->theKey = $_REQUEST["theKey"]; 39 $CourseModel->isOpen = $_REQUEST["isOpen"]; 40 $CourseModel->lastUpdateTime = time(); 41 echo $CourseModel->update(); 42 } 43 44 //删除 45 public function deleteCourseAction() 46 { 47 $ids = $_REQUEST["ids"]; 48 for($i=0;$i<count($ids);$i++) 49 { 50 $CourseModel = new CourseModel($ids[$i]); 51 if(!$CourseModel->delete()) 52 { 53 echo false; 54 return; 55 } 56 57 //删除: 58 $this->deleteService($CourseModel->defaultImg); 59 $this->deleteService($CourseModel->icon); 60 } 61 echo true; 62 } 63 64 //物理删除: 65 public function deleteService($address) 66 { 67 $file = UPLOAD_PATH . $address; 68 if (is_file($file)) { 69 # code... 70 unlink($file); 71 } 72 }
4生成器中两个字段上传图片的时候,要修改.
下面是生成器生成的:
1 //添加 2 public function addCourseAction(){ 3 $CourseModel = new CourseModel(); 4 $CourseModel->title = $_REQUEST["title"]; 5 $CourseModel->userId = $_REQUEST["userId"]; 6 if(!empty($_FILES)){ 7 $upload = new BaseUploadUtil(); 8 $upload->createPath(); 9 $upload->createDatePath(); 10 $upload->file = $_FILES["defaultImg"]; 11 $info = $upload->upload(); 12 if(count($info)>0){ 13 $CourseModel->defaultImg = $info["path"]; 14 } 15 } 16 if(!empty($_FILES)){ 17 $upload = new BaseUploadUtil(); 18 $upload->createPath(); 19 $upload->createDatePath(); 20 $upload->file = $_FILES["icon"]; 21 $info = $upload->upload(); 22 if(count($info)>0){ 23 $CourseModel->icon = $info["path"]; 24 } 25 } 26 $CourseModel->remark = $_REQUEST["remark"]; 27 $CourseModel->orderBy = $_REQUEST["orderBy"]; 28 $CourseModel->numb = $_REQUEST["numb"]; 29 $CourseModel->theKey = $_REQUEST["theKey"]; 30 $CourseModel->isOpen = $_REQUEST["isOpen"]; 31 $CourseModel->lastUpdateTime = time(); 32 echo $CourseModel->insert(); 33 } 34 //修改 35 public function updateCourseAction(){ 36 $CourseModel = new CourseModel($_REQUEST["id"]); 37 $CourseModel->title = $_REQUEST["title"]; 38 $CourseModel->userId = $_REQUEST["userId"]; 39 if(!empty($_FILES)){ 40 $upload = new BaseUploadUtil(); 41 $upload->createPath(); 42 $upload->createDatePath(); 43 $upload->file = $_FILES["defaultImg"]; 44 $info = $upload->upload(); 45 if(count($info)>0){ 46 $CourseModel->defaultImg = $info["path"]; 47 } 48 } 49 if(!empty($_FILES)){ 50 $upload = new BaseUploadUtil(); 51 $upload->createPath(); 52 $upload->createDatePath(); 53 $upload->file = $_FILES["icon"]; 54 $info = $upload->upload(); 55 if(count($info)>0){ 56 $CourseModel->icon = $info["path"]; 57 } 58 } 59 $CourseModel->remark = isset($_REQUEST["remark"]) ? $_REQUEST["remark"] : ""; 60 $CourseModel->orderBy = $_REQUEST["orderBy"]; 61 $CourseModel->numb = $_REQUEST["numb"]; 62 $CourseModel->theKey = $_REQUEST["theKey"]; 63 $CourseModel->isOpen = $_REQUEST["isOpen"]; 64 $CourseModel->lastUpdateTime = time(); 65 echo $CourseModel->update(); 66 }
上面 第 6 ,16 ,39 ,49 直接判断 $_FILES 不正确, 这里 的 有两个字段 上传 图片 . defaultImage 和 icon
应该 改为 $_FILES["defaultImage"] 和 $_FILES["icon"] .
如下:
1 //添加 2 public function addCourseAction() 3 { 4 $CourseModel = new CourseModel(); 5 $CourseModel->title = $_REQUEST["title"]; 6 $CourseModel->userId = $_REQUEST["userId"]; 7 if(!empty($_FILES["defaultImg"])){ 8 $upload = new BaseUploadUtil(); 9 $upload->createPath(); 10 $upload->createDatePath(); 11 $upload->file = $_FILES["defaultImg"]; 12 $info = $upload->upload(); 13 if(count($info)>0){ 14 $CourseModel->defaultImg = $info["path"]; 15 } 16 } 17 if(!empty($_FILES["icon"])){ 18 $upload = new BaseUploadUtil(); 19 $upload->createPath(); 20 $upload->createDatePath(); 21 $upload->file = $_FILES["icon"]; 22 $info = $upload->upload(); 23 if(count($info)>0){ 24 $CourseModel->icon = $info["path"]; 25 } 26 } 27 $CourseModel->remark = $_REQUEST["remark"]; 28 $CourseModel->orderBy = $_REQUEST["orderBy"]; 29 $CourseModel->numb = $_REQUEST["numb"]; 30 $CourseModel->theKey = $_REQUEST["theKey"]; 31 $CourseModel->isOpen = $_REQUEST["isOpen"]; 32 $CourseModel->lastUpdateTime = time(); 33 echo $CourseModel->insert(); 34 } 35 36 //修改 37 public function updateCourseAction() 38 { 39 $CourseModel = new CourseModel($_REQUEST["id"]); 40 $CourseModel->title = $_REQUEST["title"]; 41 $CourseModel->userId = $_REQUEST["userId"]; 42 if(!empty($_FILES['defaultImg'])){ 43 $upload = new BaseUploadUtil(); 44 $upload->createPath(); 45 $upload->createDatePath(); 46 $upload->file = $_FILES["defaultImg"]; 47 $info = $upload->upload(); 48 49 //删除: 50 $this->deleteService($CourseModel->defaultImg); 51 52 if(count($info)>0){ 53 $CourseModel->defaultImg = $info["path"]; 54 } 55 } 56 if(!empty($_FILES['icon'])){ 57 $upload = new BaseUploadUtil(); 58 $upload->createPath(); 59 $upload->createDatePath(); 60 $upload->file = $_FILES["icon"]; 61 $info = $upload->upload(); 62 63 //删除: 64 $this->deleteService($CourseModel->icon); 65 66 if(count($info)>0){ 67 $CourseModel->icon = $info["path"]; 68 } 69 } 70 $CourseModel->remark = isset($_REQUEST["remark"]) ? $_REQUEST["remark"] : ""; 71 $CourseModel->orderBy = $_REQUEST["orderBy"]; 72 $CourseModel->numb = $_REQUEST["numb"]; 73 $CourseModel->theKey = $_REQUEST["theKey"]; 74 $CourseModel->isOpen = $_REQUEST["isOpen"]; 75 $CourseModel->lastUpdateTime = time(); 76 echo $CourseModel->update(); 77 } 78 79 //删除 80 public function deleteCourseAction() 81 { 82 $ids = $_REQUEST["ids"]; 83 for($i=0;$i<count($ids);$i++) 84 { 85 $CourseModel = new CourseModel($ids[$i]); 86 if(!$CourseModel->delete()) 87 { 88 echo false; 89 return; 90 } 91 92 //删除: 93 $this->deleteService($CourseModel->defaultImg); 94 $this->deleteService($CourseModel->icon); 95 } 96 echo true; 97 } 98 99 //物理删除: 100 public function deleteService($address) 101 { 102 $file = UPLOAD_PATH . $address; 103 if (is_file($file)) { 104 # code... 105 unlink($file); 106 } 107 }
红色部分是修改的生成器代码:
绿色部分是添加的 物理删除 资源的方法.