Home  >  Article  >  Backend Development  >  How to achieve refresh-free upload by combining php template with js upload plug-in

How to achieve refresh-free upload by combining php template with js upload plug-in

墨辰丷
墨辰丷Original
2018-06-08 09:38:361583browse

This article mainly introduces the method of combining php template and js upload plug-in to achieve refresh-free upload. Friends who are interested can refer to it. I hope it will be helpful to everyone.

Template file code:

<!DOCTYPE html>
<html lang="cn">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <link href="<{$smarty.const.PUBLIC_PATH}>/Uploadify/uploadify.css" rel="stylesheet" type="text/css" />
  <script src="<{$smarty.const.PUBLIC_PATH}>/Uploadify/jquery.js" type="text/javascript"></script>
  <script src="<{$smarty.const.PUBLIC_PATH}>/Uploadify/jquery.uploadify.min.js" type="text/javascript"></script>
 </head>
 <script type="text/javascript">
  $(function() {
   $("#file_upload").uploadify({
    //指定swf文件
    &#39;swf&#39;: &#39;<{$smarty.const.PUBLIC_PATH}>/Uploadify/uploadify.swf&#39;,
    //后台处理的页面
    &#39;uploader&#39;: "<{U(&#39;home/Login/Uploads&#39;,&#39;&#39;,false)}>",
    //按钮显示的文字
    &#39;buttonText&#39;: &#39;上传图片&#39;,
     //显示的高度和宽度
    "height" : 30,
    &#39;fileTypeDesc&#39;: &#39;Image Files&#39;,
    //允许上传的文件后缀
    &#39;fileTypeExts&#39;: &#39;*.gif; *.jpg; *.png&#39;,
    //发送给后台的其他参数通过formData指定
    //&#39;formData&#39;: { &#39;someKey&#39;: &#39;someValue&#39;, &#39;someOtherKey&#39;: 1 },
    "method" : &#39;post&#39;,//方法,服务端可以用$_POST数组获取数据
    &#39;removeTimeout&#39;  : 1,
    "onUploadSuccess" : uploadPicture
  });
  //可以根据自己的要求来做相应处理
  function uploadPicture(file, data){
    var data = eval(&#39;(&#39; + data + &#39;)&#39;);
   if(data.errorcode){
    alert(data.errormsg); 
   } else {
    alert(data.errormsg);
   }
  } 
 });
</script>
 <body>
  <input type="file" name="file_upload" id="file_upload" />
 </body>
</html>

Controller code:

public function uploads(){
  $arr = array( "errorcode"=>"1","errormsg"=>"上传成功!");
  $model = M(&#39;applicant&#39;);
  if (!empty($_FILES)) {
    //图片上传设置
    $config = array( 
    &#39;maxSize&#39; => 1000000, 
    &#39;rootPath&#39; => &#39;Public&#39;,
    &#39;savePath&#39; => &#39;/Uploads/&#39;, 
    &#39;saveName&#39; => array(&#39;uniqid&#39;,&#39;&#39;), 
    &#39;exts&#39;  => array(&#39;jpg&#39;, &#39;gif&#39;, &#39;png&#39;, &#39;jpeg&#39;), 
    &#39;autoSub&#39; => false, 
    &#39;subName&#39; => array(&#39;date&#39;,&#39;Ymd&#39;),
   );
   $upload = new \Think\Upload($config);// 实例化上传类
  $info = $upload->upload();
   if($info){
    $arr[&#39;errorcode&#39;] = "0";
   } else {
    $arr["errorcode"] = "1";
    $arr["errormsg"] = $upload->getError();
   }
   /* 返回JSON数据 */
   $this->ajaxReturn($arr);
  }
}

Summary: The above is the entire content of this article, I hope it can be helpful to everyone Learning helps.

Related recommendations:

PHP View the background, process and solution of the current variable type

php is based on Imagick implements the function of adding watermarks and text to images

php implements the function of the judges' scorer

The above is the detailed content of How to achieve refresh-free upload by combining php template with js upload plug-in. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn