先看html加个表单,注意这里的action 路径要选 对。
<div> <form action="__URL__/add_img" enctype="multipart/form-data" method="post" style="padding:10px;" > 图片宽度:<input type="text" name="width" /> 图片长度:<input type="text" name="height" /> <input type="file" name="photo" /> <input type="submit" value="提交" > </form> </div>
然后在控制器里加如下代码:
function add_img() { if($_POST['width']&&$_POST['height']) { $rs=import('ORG.Net.UploadFile'); $upload=new UploadFile(); $upload->maxSize=2097153; //设置上传文件大小为2M $root_path=BATH_PATH; //等下好上传 $sub_path='/image/wapphoto/';//一切为了上传 $upload->savePath =BATH_PATH .'/image/wapphoto/';// 设置附件上传目录 $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 $upload->thumb = true; $upload->thumbMaxWidth = $_POST['width']; $upload->thumbMaxHeight = $_POST['height']; $file->thumbPath=BATH_PATH .'/image/wapphoto/';//缩略图保存路径 $file->thumbRemoveOrigin=true;//生成缩略图是否删除原图片 if(!$upload->upload()) {// 上传错误提示错误信息 $this->error($upload->getErrorMsg()); }else{// 上传成功 获取上传文件信息 $info = $upload->getUploadFileInfo(); // $size=$_POST['width'].'*'.$_POST['height']; // print_r($info); $model=M('wap_photo'); $model->photo=$sub_path.'thumb_'. $info[0]["savename"]; $model->size=$_POST['width'].'*'.$_POST['height']; $model->time=time(); $model->add(); $this->success("数据保存成功!"); } }else { echo '上传失败'; return false; } }