add_news.php
<include file="Public:header"/> <form id="myform" method="post" action="{pigcms{:U('Systemvoicedetails/add_news')}" frame="true" refresh="true"> <table cellpadding="0" cellspacing="0" class="frame_form" width="100%"> <tr> <th width="80">标题</th> <td><input type="text" class="input fl" name="title" size="75" placeholder="标题" validate="maxlength:50,required:true"/></td> </tr> <tr> <th width="80">列表文章ID</th> <td> <input type="hidden" name="pid" class="pid"/><br/> <input type="text" name="pid2" class="pid2" style="width:300px;height:20px;"/><br/> <div id="returngetData" style="display:none;position:fixed;background:white;border:1px solid blue"></div> </td> </tr> <tr> <th width="80">语种</th> <td> <select name="language"> <option value="中文">中文</option> <option value="英文">英文</option> <option value="韩文">韩文</option> <option value="日文">日文</option> </select> </td> </tr> <tr> <th width="80">声音</th> <td> <select name="sex"> <option value="女">女</option> <option value="男">男</option> </select> </td> </tr> <tr> <th width="80">简介</th> <td><input type="text" class="input fl" name="introduction" size="75" placeholder="简介" validate="maxlength:50,required:true"/></td> </tr> <tr> <th width="80">分类</th> <if condition="$category"> <td> <select name="category_id"> <volist name="category" id="vo"> <option value="{pigcms{$vo.id}" <if condition="$vo['id'] eq $_GET['category_id']">selected="selected"</if>>{pigcms{$vo.name}</option> </volist> </select> </td> </if> </tr> <tr> <th width="80">排序</th> <td><input type="text" class="input fl" name="sort" value="0" placeholder="快报标题" validate="maxlength:50,required:true,digits:true"/></td> </tr> <tr> <th width="80">经纬度</th> <td id="choose_map2"></td> </tr> <!--<tr> <th width="80">添加图片1</th> <td> <input type="input" id="img4" name="img4" style="width:200px"/> <a href="javascript:void(0)" class="btn btn-sm btn-success" id="J_selectImage" οnclick="window.top.artiframe('{pigcms{:U('upload',array('imgid'=>4,'id'=>$news["id"],'iframename'=>'Openadd_news','frame_show'=>true))}','上传图片',520,480,true,false,false,false,'detail',true);" >上传图片</a> <span class="form_tips" style="color:red">APP主图</span> </td> </tr> <tr> <th width="80">添加图片2</th> <td> <input type="input" id="img5" name="img5" style="width:200px"/> <a href="javascript:void(0)" class="btn btn-sm btn-success" οnclick="window.top.artiframe('{pigcms{:U('upload',array('imgid'=>5,'id'=>$news["id"],'iframename'=>'Openadd_news','frame_show'=>true))}','查看分类信息',520,480,true,false,false,false,'detail',true);" >上传图片</a> <span class="form_tips" style="color:red">APP首页位置头像图片</span> </td> </tr> <tr> <th width="80">添加图片3</th> <td> <input type="input" id="img6" name="img6" style="width:200px"/> <a href="javascript:void(0)" class="btn btn-sm btn-success" οnclick="window.top.artiframe('{pigcms{:U('upload',array('imgid'=>6,'id'=>$news["id"],'iframename'=>'Openadd_news','frame_show'=>true))}','查看分类信息',520,480,true,false,false,false,'detail',true);" >上传图片</a> <span class="form_tips" style="color:red">APP语音导航顶部位置六个图片</span> </td> </tr> <tr> <th width="80">添加图片4</th> <td> <input type="input" id="img7" name="img7" style="width:200px"/> <a href="javascript:void(0)" class="btn btn-sm btn-success" οnclick="window.top.artiframe('{pigcms{:U('upload',array('imgid'=>7,'id'=>$news["id"],'iframename'=>'Openadd_news','frame_show'=>true))}','查看分类信息',520,480,true,false,false,false,'detail',true);" >上传图片</a> </td> </tr> <tr> <th width="80">添加图片5</th> <td> <input type="input" id="img8" name="img8" style="width:200px"/> <a href="javascript:void(0)" class="btn btn-sm btn-success" οnclick="window.top.artiframe('{pigcms{:U('upload',array('imgid'=>8,'id'=>$news["id"],'iframename'=>'Openadd_news','frame_show'=>true))}','查看分类信息',520,480,true,false,false,false,'detail',true);" >上传图片</a> </td> </tr>--> <input type="hidden" id="newbut"> <tr> <th width="80">添加图片</th> <td> <div id="displayimgs"></div> <input type="button" id="tjtp" value="添加图片" /> <span id="res"> 添加成功 </span> </td> </tr> <tr> <th width="80">内容</th> <td> <textarea name="content" id="content"></textarea> </td> </tr> <tr> <th width="80">状态</th> <td> <span class="cb-enable"><label class="cb-enable selected"><span>启用</span><input type="radio" name="status" value="1" checked="checked"/></label></span> <span class="cb-disable"><label class="cb-disable "><span>禁用</span><input type="radio" name="status" value="0" /></label></span> </td> </tr> </table> <div class="btn hidden"> <input type="submit" name="dosubmit" id="dosubmit" value="提交" class="button" /> <input type="reset" value="取消" class="button" /> </div> </form> <form enctype ="multipart/form-data" method="post" id="fileForm" > <input type="hidden" id="newfile"> </form> <script src="{pigcms{$static_public}kindeditor/kindeditor.js"></script> <script type="text/javascript"> var a = 0; //点击添加图片 ,需要在两个form都加入一个 //一个显示 $("#tjtp").click( function(){ //alert(a); //$("$newbut").after('<tr><th width="80">添加图片'+a+'</th><td><a class="vb1-btn-blue vb1-right" οnclick="javascript:openFileBrowse(Filedata);" href="#">导入人群</a></td></tr>'); //alert(1); //显示的按钮 $("#newbut").after('<tr><th width="80">添加图片'+a+'</th><td><a οnclick="javascript:openFileBrowse('+a+');">添加图片'+a+'</a></td></tr>'); $("#newfile").after('<input type="file" style="display:none;" name="uploadedfile'+a+'" id="Filedata'+a+'" οnchange="javascript:uploadFile('+a+')"/>'); a++; }) //打开文件选择框 function openFileBrowse(fileId){ fileId='Filedata'+fileId; //alert(fileId); var ie=navigator.appName=="Microsoft Internet Explorer" ? true : false; if(ie){ document.getElementById(fileId).click(); }else{ var a=document.createEvent("MouseEvents");//FF的处理 a.initEvent("click", true, true); document.getElementById(fileId).dispatchEvent(a); } } //提交上传 function uploadFile() { var formData = new FormData($( "#fileForm" )[0]); //alert(formData); $.ajax({ url: 'http://localhost/upload.php' , type: 'POST', json: 'dmpcallback', data: formData, dataType: "json", async: false, cache: false, contentType: false, processData: false, success: function (result) { alert('成功'); $('#res').html(''); var str = '成功'; $.each(result.result,function(i,e){ //alert(i); //alert(e); //str += '<input type="hidden" name="'+i+'" value="'+e+'" />'; str += '<input type="hidden" name="img[]" value="'+e+'" />'; }) //alert(str); $('#res').html(str); //alert(result.result); //result.result.each(function(index,element){ //alert(index); //}) //提交成功的文件全部放到input hidden /*if(result.ret==1){ alert('上传成功'); window.location.reload(); }else{ if(result.ret==0 && result.msg){ alert(result.msg); }else { alert('上传失败1'); } }*/ }, error: function (result) { $('#res').html('成功'); //alert('失败'); } }); } KindEditor.ready(function(K){ kind_editor = K.create("#content",{ width:'402px', height:'320px', resizeType : 1, <if condition="$_GET['frame_show']">readonlyMode : true,</if> allowPreviewEmoticons:false, allowImageUpload : true, filterMode: true, items : [ 'source', 'fullscreen', '|', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist', '|', 'emoticons', 'image', 'link' ], emoticonsPath : './static/emoticons/', uploadJson : "{pigcms{$config.site_url}/index.php?g=Index&c=Upload&a=editor_ajax_upload&upload_dir=system/news" }); }); $(function(){ $(".pid2").blur(function(){ $("#returngetData").css("display","block"); var pid = $(".pid2").val(); $.ajax({ type:"POST", url:"{pigcms{:U('Systemvoicedetails/l_ajax')}", data:{"pid":pid}, cache:false, dataType:"json", success:function(data2){ $('#returngetData').empty(); for(var i=0;i<data2.result.length;i++){ $("#returngetData").append("<div class='rdate' id='"+data2['result'][i]['title']+"' value='"+data2['result'][i]['id']+"'>"+data2['result'][i]['id']+" "+data2['result'][i]['title']+"</div>"); } $(".rdate").click(function(){ var rtitle = $(this).attr('value'); var rtitle2 = $(this).attr('id'); $(".pid").attr('value',rtitle); $(".pid2").attr('value',rtitle2); $("#returngetData").css("display","none"); }); } }); }); }); //function clock(){ //alert("xx"); //$.post('{pigcms{:U('ajaximgs')}',function(data){ //alert(data.result.img4); //if(data.result.img4!='') $("#img4").val(data.result.img4); //if(data.result.img5!='') $("#img5").val(data.result.img5); //if(data.result.img6!='') $("#img6").val(data.result.img6); //},'json'); //} //setInterval("clock()",1000) //function clock2(){ //alert("xx"); //$.post('{pigcms{:U('ajaxposition')}',function(data){ //alert(data.result.long_lat); //if(data.result.long_lat!='')$("#long_lat").val(data.result.long_lat); //},'json'); //} //var ib = setInterval("clock2()",10) //clearInterval(ia); </script> <include file="Public:footer"/> upload.php <?php //遍历文件数组,写入服务器 //这边做一个记录 哪个成功 哪个失败 //var_dump($_FILES); $url = "http://".$_SERVER['SERVER_NAME']."/upload/ftp/"; $local = 'C:\\ym\\o2o\\upload\\ftp\\'; $ok=array(); foreach($_FILES as $k=>$v){ if($v!=null){ if(!is_file($local.$_FILES[$k]["name"])){ //var_dump($k); $b = file_put_contents($local.$_FILES[$k]["name"],file_get_contents($_FILES[$k]["tmp_name"])); if($b == false){ $ok[$k]=''; }else{ $ok[$k]=$url.$_FILES[$k]["name"]; } }else{ $ok[$k]=$url.$_FILES[$k]["name"]; } } } exit(json_encode(array('error_code'=>0,'error_reason'=>'成功','result'=>$ok))); /*$redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo $_POST['blob_num'].'_'.$_POST['file_name']; $redis->set($_POST['blob_num'].'_'.$_POST['file_name'], file_get_contents($_FILES["uploadedfile"]["tmp_name"])); */ /* $conn = ftp_connect("localhost") or die("Could not connect"); var_dump($conn); echo ftp_login($conn,"test","test"); var_dump($_FILES["uploadedfile"]); echo $_POST['blob_num']; echo "<br />"; echo $_POST['total_blob_num']; echo "<br />"; echo $_POST['file_name']; echo "<br />"; echo ftp_put($conn,$_POST['blob_num'].'_'.$_POST['file_name'],$_FILES["uploadedfile"]["tmp_name"],FTP_BINARY); ftp_close($conn);*/ /*$conn = ftp_connect("localhost") or die("Could not connect"); var_dump($conn); echo ftp_login($conn,"test","test"); var_dump($_FILES["uploadedfile"]); echo ftp_put($conn,$_FILES["uploadedfile"]["name"],$_FILES["uploadedfile"]["tmp_name"],FTP_BINARY); ftp_close($conn);*/ ?> 控制器:SystemvoicedetailsAction.class.php public function add_news(){ $this->assign("long_lat2",$long_lat2); if(IS_POST){ $data['title'] = $_POST['title']; $map['title'] = $_POST['title']; $data['introduction'] = $_POST['introduction']; $map['introduction'] = $_POST['introduction']; if(empty($_POST['content'])){ $this->error('内容不能为空!'); } $data['content'] = htmlspecialchars_decode($_POST['content']); $map['content'] = htmlspecialchars_decode($_POST['content']); $data['sort'] = $_POST['sort']; $map['sort'] = $_POST['sort']; $data['status'] = $_POST['status']; $map['status'] = $_POST['status']; $data['category_id'] = $_POST['category_id']; $map['category_id'] = $_POST['category_id']; $data['language'] = $_POST['language']; $map['language'] = $_POST['language']; $data['sex'] = $_POST['sex']; $map['sex'] = $_POST['sex']; $data['pid'] = $_POST['pid']; $map['pid'] = $_POST['pid']; $data['add_time'] = $data['last_time']=time(); //$url="http://localhost/index.php?g=wap&c=Systemnews1&a=news&id=63"; //file_get_contents() 函数把整个文件读入一个字符串中 //$string=file_get_contents($url); //preg_match_all函数进行全局正则表达式匹配。 preg_match_all("/<img([^>]*)\s*src=('|\")([^'\"]+)('|\")/", $data['content'],$matches);//带引号 //preg_match_all("/<img([^>]*)\ssrc=([^\s>]+)/",$string,$matches);//不带引号 $new_arr=array_unique($matches[0]);//去除数组中重复的值 foreach($new_arr as $k=>$key){ //echo $key."</br>"; preg_match_all('#src\s*=\s*["\']?([^\s"\']*)#i', $key, $match); foreach($match[1] AS $v) { $temp = 'img'.($k+1); $data[$temp]=$v; } } //$data['img4'] = $_REQUEST['img4']; //$data['img5'] = $_REQUEST['img5']; //$data['img6'] = $_REQUEST['img6']; //$data['img7'] = $_REQUEST['img7']; //$data['img8'] = $_REQUEST['img8']; $long_lat = explode(',',$_REQUEST['long_lat']); $data['long'] = $long_lat[0]; $data['lat'] = $long_lat[1]; if(D('system_voice_details')->add($data)){ $id = M('system_voice_details')->where($map)->getField('id'); $map_img['svdid']=array('eq',$id); M('system_voice_details_imgs')->where($map_img)->delete(); foreach($_POST['img'] as $k=>$v){ $data_img['svdid']=$id; $data_img['path']=$v; var_dump($data_img); echo M('system_voice_details_imgs')->add($data_img); } exit; //session_start(); //session('img4',null); //session('img5',null); //session('img6',null); $this->success('添加成功!'); }else{ $this->error('添加失败!'); } }else { $category = D('system_voice_details_category')->select(); $this->assign("category",$category); $this->display(); } } 数据库: