1) 上传多张图片时 ,对 $_FILES 的处理. upload ; 2)fileinput 上传多张图片. 3) 修改,删除的时候删除原来的资源,图片 update, delete , 删除 4)生成器中两个字段上传图片的时候,要修改生成器生成的代码

时间:2022-04-10 12:25:31

1上传多张图片, 要对 $_FILES进行 重新处理.

     //添加
public function addCourseAlbumAction()
{
$CourseAlbumModel = new CourseAlbumModel();
$CourseAlbumModel->title = $_REQUEST["title"];
$CourseAlbumModel->courseId = $_REQUEST["courseId"];
if(!empty($_FILES))
{ $tempArr = $_FILES["url"];
$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->url = $info["path"];
}
$CourseAlbumModel->orderBy = $_REQUEST["orderBy"];
$CourseAlbumModel->lastUpdateTime = time();
$CourseAlbumModel->insert();
} echo 1; }
else
{
$CourseAlbumModel->orderBy = $_REQUEST["orderBy"];
$CourseAlbumModel->lastUpdateTime = time();
echo $CourseAlbumModel->insert();
} }

关键代码:

             $tempArr = $_FILES["url"];
$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(); }

处理后的 数组是  $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

     //修改
public function updateCourseAction()
{
$CourseModel = new CourseModel($_REQUEST["id"]);
$CourseModel->title = $_REQUEST["title"];
$CourseModel->userId = $_REQUEST["userId"];
if(!empty($_FILES['defaultImg'])){
$upload = new BaseUploadUtil();
$upload->createPath();
$upload->createDatePath();
$upload->file = $_FILES["defaultImg"];
$info = $upload->upload(); //删除:
$this->deleteService($CourseModel->defaultImg); if(count($info)>0){
$CourseModel->defaultImg = $info["path"];
}
}
if(!empty($_FILES['icon'])){
$upload = new BaseUploadUtil();
$upload->createPath();
$upload->createDatePath();
$upload->file = $_FILES["icon"];
$info = $upload->upload(); //删除:
$this->deleteService($CourseModel->icon); if(count($info)>0){
$CourseModel->icon = $info["path"];
}
}
$CourseModel->remark = isset($_REQUEST["remark"]) ? $_REQUEST["remark"] : "";
$CourseModel->orderBy = $_REQUEST["orderBy"];
$CourseModel->numb = $_REQUEST["numb"];
$CourseModel->theKey = $_REQUEST["theKey"];
$CourseModel->isOpen = $_REQUEST["isOpen"];
$CourseModel->lastUpdateTime = time();
echo $CourseModel->update();
} //删除
public function deleteCourseAction()
{
$ids = $_REQUEST["ids"];
for($i=0;$i<count($ids);$i++)
{
$CourseModel = new CourseModel($ids[$i]);
if(!$CourseModel->delete())
{
echo false;
return;
} //删除:
$this->deleteService($CourseModel->defaultImg);
$this->deleteService($CourseModel->icon);
}
echo true;
} //物理删除:
public function deleteService($address)
{
$file = UPLOAD_PATH . $address;
if (is_file($file)) {
# code...
unlink($file);
}
}

4生成器中两个字段上传图片的时候,要修改.

下面是生成器生成的:

     //添加
public function addCourseAction(){
$CourseModel = new CourseModel();
$CourseModel->title = $_REQUEST["title"];
$CourseModel->userId = $_REQUEST["userId"];
6 if(!empty($_FILES)){
$upload = new BaseUploadUtil();
$upload->createPath();
$upload->createDatePath();
$upload->file = $_FILES["defaultImg"];
$info = $upload->upload();
if(count($info)>0){
$CourseModel->defaultImg = $info["path"];
}
}
16 if(!empty($_FILES)){
$upload = new BaseUploadUtil();
$upload->createPath();
$upload->createDatePath();
$upload->file = $_FILES["icon"];
$info = $upload->upload();
if(count($info)>0){
$CourseModel->icon = $info["path"];
}
}
$CourseModel->remark = $_REQUEST["remark"];
$CourseModel->orderBy = $_REQUEST["orderBy"];
$CourseModel->numb = $_REQUEST["numb"];
$CourseModel->theKey = $_REQUEST["theKey"];
$CourseModel->isOpen = $_REQUEST["isOpen"];
$CourseModel->lastUpdateTime = time();
echo $CourseModel->insert();
}
//修改
public function updateCourseAction(){
$CourseModel = new CourseModel($_REQUEST["id"]);
$CourseModel->title = $_REQUEST["title"];
$CourseModel->userId = $_REQUEST["userId"];
39 if(!empty($_FILES)){
$upload = new BaseUploadUtil();
$upload->createPath();
$upload->createDatePath();
$upload->file = $_FILES["defaultImg"];
$info = $upload->upload();
if(count($info)>0){
$CourseModel->defaultImg = $info["path"];
}
}
49 if(!empty($_FILES)){
$upload = new BaseUploadUtil();
$upload->createPath();
$upload->createDatePath();
$upload->file = $_FILES["icon"];
$info = $upload->upload();
if(count($info)>0){
$CourseModel->icon = $info["path"];
}
}
$CourseModel->remark = isset($_REQUEST["remark"]) ? $_REQUEST["remark"] : "";
$CourseModel->orderBy = $_REQUEST["orderBy"];
$CourseModel->numb = $_REQUEST["numb"];
$CourseModel->theKey = $_REQUEST["theKey"];
$CourseModel->isOpen = $_REQUEST["isOpen"];
$CourseModel->lastUpdateTime = time();
echo $CourseModel->update();
}

上面 第 6 ,16 ,39 ,49 直接判断 $_FILES 不正确,  这里 的 有两个字段 上传 图片 .  defaultImage 和 icon

应该 改为  $_FILES["defaultImage"]   和  $_FILES["icon"] .

如下:

     //添加
public function addCourseAction()
{
$CourseModel = new CourseModel();
$CourseModel->title = $_REQUEST["title"];
$CourseModel->userId = $_REQUEST["userId"];
7 if(!empty($_FILES["defaultImg"])){
$upload = new BaseUploadUtil();
$upload->createPath();
$upload->createDatePath();
$upload->file = $_FILES["defaultImg"];
$info = $upload->upload();
if(count($info)>0){
$CourseModel->defaultImg = $info["path"];
}
}
17 if(!empty($_FILES["icon"])){
$upload = new BaseUploadUtil();
$upload->createPath();
$upload->createDatePath();
$upload->file = $_FILES["icon"];
$info = $upload->upload();
if(count($info)>0){
$CourseModel->icon = $info["path"];
}
}
$CourseModel->remark = $_REQUEST["remark"];
$CourseModel->orderBy = $_REQUEST["orderBy"];
$CourseModel->numb = $_REQUEST["numb"];
$CourseModel->theKey = $_REQUEST["theKey"];
$CourseModel->isOpen = $_REQUEST["isOpen"];
$CourseModel->lastUpdateTime = time();
echo $CourseModel->insert();
} //修改
public function updateCourseAction()
{
$CourseModel = new CourseModel($_REQUEST["id"]);
$CourseModel->title = $_REQUEST["title"];
$CourseModel->userId = $_REQUEST["userId"];
42 if(!empty($_FILES['defaultImg'])){
$upload = new BaseUploadUtil();
$upload->createPath();
$upload->createDatePath();
$upload->file = $_FILES["defaultImg"];
$info = $upload->upload(); 49 //删除:
50 $this->deleteService($CourseModel->defaultImg); if(count($info)>0){
$CourseModel->defaultImg = $info["path"];
}
}
56 if(!empty($_FILES['icon'])){
$upload = new BaseUploadUtil();
$upload->createPath();
$upload->createDatePath();
$upload->file = $_FILES["icon"];
$info = $upload->upload(); 63 //删除:
64 $this->deleteService($CourseModel->icon); if(count($info)>0){
$CourseModel->icon = $info["path"];
}
}
$CourseModel->remark = isset($_REQUEST["remark"]) ? $_REQUEST["remark"] : "";
$CourseModel->orderBy = $_REQUEST["orderBy"];
$CourseModel->numb = $_REQUEST["numb"];
$CourseModel->theKey = $_REQUEST["theKey"];
$CourseModel->isOpen = $_REQUEST["isOpen"];
$CourseModel->lastUpdateTime = time();
echo $CourseModel->update();
} //删除
public function deleteCourseAction()
{
$ids = $_REQUEST["ids"];
for($i=0;$i<count($ids);$i++)
{
$CourseModel = new CourseModel($ids[$i]);
if(!$CourseModel->delete())
{
echo false;
return;
} 92 //删除:
93 $this->deleteService($CourseModel->defaultImg);
94 $this->deleteService($CourseModel->icon);
}
echo true;
} //物理删除:
public function deleteService($address)
{
$file = UPLOAD_PATH . $address;
if (is_file($file)) {
# code...
unlink($file);
}
}

红色部分是修改的生成器代码:

绿色部分是添加的 物理删除 资源的方法.