1) 上传多张图片时 ,对 $_FILES 的处理. upload ; 2)fileinput 上传多张图片. 3) 修改,删除的时候删除原来的资源,图片 update, delete , 删除 4)生成器中两个字段上传图片的时候,要修改生成器生成的代码
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);
}
}
红色部分是修改的生成器代码:
绿色部分是添加的 物理删除 资源的方法.
1) 上传多张图片时 ,对 $_FILES 的处理. upload ; 2)fileinput 上传多张图片. 3) 修改,删除的时候删除原来的资源,图片 update, delete , 删除 4)生成器中两个字段上传图片的时候,要修改生成器生成的代码的更多相关文章
- sql一个表中两个字段合并求和
sql一个表中两个字段,合并求和 SELECT SUM(字段a+'.'+字段b) as total from TABLE
- 一条SQL语句查询两表中两个字段
首先描述问题,student表中有字段startID,endID.garde表中的ID需要对应student表中的startID或者student表中的endID才能查出grade表中的name字段, ...
- oracle中的timestamp字段的值乱码问题修改
我的解决方案: 直接新增一个系统变量: key值为:NLS_TIMESTAMP_FORMATvalue的值为:YYYY-MM-DD HH24:MI:SSFF6 其它解决方案: 在登录PLSQL之后,查 ...
- Vue 两个字段联合校验典型例子--修改密码
1.前言 本文是前文<Vue Element-ui表单校验规则,你掌握了哪些?>针对多字段联合校验的典型应用. 在修改密码时,一般需要确认两次密码一致,涉及2个属性字段.类似的涉及 ...
- sql一张表中两个字段指向同一个外键
在项目开发中遇到这么一个例子,首先产品表 tb_product ----------------------------- id name 1 手机 2 电脑 3 笔记本 ...
- MySQL 一张表中两个字段值互换
update table a, table b set a.filed1= b.field2, a.field2= b.field1where a.id = b.id
- SQL Server 2008中SQL增强之三:Merge(在一条语句中使用Insert,Update,Delete) 一条语句实现两表同步(添加、删除、修改)
MERGE 目标表 USING 源表 ON 匹配条件 WHEN MATCHED THEN 语句 WHEN NOT MATCHED THEN 语句; http://www.chinaz.com/prog ...
- ASP.NET Core WEB API 使用element-ui文件上传组件el-upload执行手动文件文件,并在文件上传后清空文件
前言: 从开始学习Vue到使用element-ui-admin已经有将近快两年的时间了,在之前的开发中使用element-ui上传组件el-upload都是直接使用文件选取后立即选择上传,今天刚好做了 ...
- RENIX报文两个字段嵌套变化——网络测试仪实操
RENIX软件如何实现报文中两个字段嵌套变化,以下为您实操讲解详细步骤. 1.打开Renix软件,连接机框并预约测试端口: 2.创建一条RAW流量(Binding流量也可以,这里用RAW流做例子) 3 ...
随机推荐
- python and or的理解规则
>>> 'a' and 'b' 'b' >>> '' and 'b' '' >>> 'a' and 'b' and 'c' 'c’ 解释:在布尔上 ...
- Python操作excel的几种方式--xlrd、xlwt、openpyxl
openpyxl xlrd xlwt 在处理excel数据时发现了xlwt的局限性–不能写入超过65535行.256列的数据(因为它只支持Excel 2003及之前的版本,在这些版本的Excel中 ...
- 海康摄像头配置固定IP
前言 首先要海康设备连接好网线,电脑客户端跟海康设备在同一个局域网络. 1.直接在海康网站下载SADP工具软件,安装SADP工具,如图所示: 2.安装成功后,桌面的出现设备网络搜索, 面板介绍:这里将 ...
- 廖雪峰网站:学习python基础知识—list和tuple(二)
1.list """ Python内置的一种数据类型是列表:list. list是一种有序的集合,可以随时添加和删除其中的元素. """ c ...
- DOM与document的区别
DOM: DOM 全称是 Document Object Model,也就是文档对象模型. DOM 就是针对 HTML 和 XML 提供的一个API.什么意思?就是说为了能以编程的方法操作这个 HTM ...
- 牛客练习赛32-D-MST+tarjin割边
链接:https://ac.nowcoder.com/acm/contest/272/D来源:牛客网 题目描述 小p和他的朋友约定好去游乐场游玩,但是他们到了游乐场后却互相找不到对方了. 游乐场可以看 ...
- InnoDB存储引擎介绍-(4)Checkpoint机制一
检查点的工作机制: innodb会自动维护一个检查点的机制,叫做 fuzzy checkpointing(当然sharp checkpoint也是检查点之一),fuzzy checkpointing就 ...
- -bash: /etc/init.d/nginx: /bin/bash^M: bad interpreter: No such file or directory
-bash: /etc/init.d/nginx: /bin/bash^M:bad interpreter: No such file or directory 这个使为了弄nginx自启的,然后在官 ...
- CP-ABE ToolKit 安装笔记(转载)
博主论文狗,好久没有来贴博客,最近做实验需要用到属性加密,了解了下CP-ABE,前来记录一下: 网上相关的博文较多,博主看了大部分的,认为下面这两个看完了基本就可以成功安装. 可参见博文: http: ...
- curl和wget的区别和使用
curl和wget基础功能有诸多重叠,如下载等. 非要说区别的话,curl由于可自定义各种请求参数所以在模拟web请求方面更擅长:wget由于支持ftp和Recursive所以在下载文件方面更擅长.类 ...