>  기사  >  백엔드 개발  >  Layui 프레임워크는 파일 업로드를 구현하고 TP3.2.3(thinkPHP)은 업로드된 파일에 대한 백그라운드 처리 작업을 수행합니다.

Layui 프레임워크는 파일 업로드를 구현하고 TP3.2.3(thinkPHP)은 업로드된 파일에 대한 백그라운드 처리 작업을 수행합니다.

不言
不言원래의
2018-06-01 09:30:532476검색

이 글에서는 Layui 프레임워크에 의한 파일 업로드 구현과 TP3.2.3에 의한 업로드된 파일의 백그라운드 처리 작업을 주로 소개합니다. 파일 업로드 및 처리 작업을 위해 thinkPHP와 결합된 Layui 프레임워크의 관련 구현 기술을 예제 형식으로 분석합니다. 도움이 필요한 친구가 참고할 수 있습니다

이 기사의 예는layui 프레임워크가 파일 업로드를 구현하고 TP3.2.3이 업로드된 파일에 대해 백그라운드 처리 작업을 수행하는 방법을 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 세부 사항은 다음과 같습니다.

layui 프레임워크는 버전 1.0.9입니다. .

먼저 HTML 페이지 코드는 다음과 같습니다.

<p class="layui-form-item" id="upload_file">
     <p class="layui-input-block" style="width: 300px;">
      <input type="hidden" id="img_url1" name="HeadImageUrl" value=""/>
      <p class="layui-upload-drag" id="uploadpic1" lay-verify="uploadpic1">
        <p class="layui-col-xs12 layui-col-md12">
          <img class="layui-upload-img" id="demo1" >
        </p>
        <p class="button-hide">
          <input type="file" name="banner_file_upload" id="banner_file_upload" class="layui-uplaod-file"  lay-type="file">
        </p>
      </p>
    </p>
</p>

js 코드는 다음과 같습니다.

<script type="text/javascript" th:inline="javascript">
     layui.use(&#39;upload&#39;, function (){
       var upload = layui.upload;
       var url="/Public";
       upload({
         elem: &#39;#banner_file_upload&#39;,
         url: "/index.php/Admin/Product/upload",
         method: &#39;post&#39;,
         before: function(obj){
           console.log(&#39;文件上传中&#39;);
           layer.load();
         },
         success: function (msg) {
           console.log(msg);
           if(msg.msg=="success"){
             layer.closeAll(&#39;loading&#39;);
             layer.msg("上传成功");
             $("#img_url1").attr("value", msg.src);
           }else if(msg.msg=="error"){
             layer.closeAll(&#39;loading&#39;);
             layer.msg(msg.code);
           }
         },
         error:function (data) {
           layer.msg("上传失败");
           console.log(data);
         }
       });
     });
</script>

PHP 배경에서 값을 받는 다음 방법:

#上传文件方法
public function upload(){
    $res=array(
     &#39;code&#39;=>1,
     &#39;msg&#39;=>&#39;no sorry&#39;,
      &#39;data&#39;=>array(
        &#39;src&#39;=>&#39;&#39;,
      )
    );
    #图片存放路径
    $directory = C(&#39;UPLOAD_PATH&#39;)."/Public/docment/";
    #判断目录是否存在 不存在则创建
    if(!(is_dir($directory))){
      $this->directory($directory);
    }
    #获取数据库最后一条id 当做文件名称
    $product_last_id=D(&#39;ApiProduct&#39;)->getLastId();
    $savename="ApiProduct_".time().&#39;_&#39;.($product_last_id[&#39;id&#39;]+1);
    $upload = new \Think\Upload();
    $upload->maxSize = 0;
    $upload->exts = array(&#39;doc&#39;,&#39;docx&#39;,&#39;xls&#39;,&#39;xlsx&#39;,&#39;pdf&#39;,&#39;txt&#39;);
    $upload->rootPath = $directory;
    $upload->saveName="$savename";
    $upload->savePath = &#39;&#39;;
    $info = $upload->uploadOne($_FILES[&#39;banner_file_upload&#39;]);
    if(!$info){
      $res[&#39;code&#39;]=$upload->getError();
      $res[&#39;msg&#39;]=&#39;error&#39;;
    }else{
      $res[&#39;code&#39;]=0;
      $res[&#39;msg&#39;]=&#39;success&#39;;
      $res[&#39;src&#39;]="/Public/docment/".$savename.".".$info[&#39;ext&#39;];
    }
   echo json_encode($res);die;
}
/**
* 递归创建文件
* @author erwa<erwa@qingjinju.net>
*/
public function directory($dir){
    return is_dir ( $dir ) or directory(dirname( $dir )) and mkdir ( $dir , 0777);
}

이것이 이 글의 전체 내용입니다. 감사합니다. 읽어주셔서 감사합니다. 자세한 내용은 PHP 중국어 웹사이트를 참고하세요!

관련 권장 사항:

layui는 동적 및 정적 데이터 테이블의 페이징을 구현합니다

위 내용은 Layui 프레임워크는 파일 업로드를 구현하고 TP3.2.3(thinkPHP)은 업로드된 파일에 대한 백그라운드 처리 작업을 수행합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.