Home Backend Development PHP Tutorial Layui framework implements file upload and TP3.2.3 background processing operation example for uploaded files

Layui framework implements file upload and TP3.2.3 background processing operation example for uploaded files

Jun 23, 2018 pm 05:04 PM
layui framework thinkphp Background processing File Upload

This article mainly introduces the layui framework to implement file upload and TP3.2.3 to perform background processing operations on uploaded files. It analyzes the related implementation techniques of layui framework combined with thinkPHP for file upload and processing operations in the form of examples. Friends in need can refer to Next

The example of this article describes the implementation of file upload by layui framework and the background processing operation of uploaded files by TP3.2.3. Share it with everyone for your reference, the details are as follows:

layui framework is version 1.0.9. .

First, the html page code is as follows:

<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>
Copy after login

js code is as follows:

<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>
Copy after login

The next method of receiving values ​​​​from the php background:

#上传文件方法
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);
}
Copy after login

## Articles you may be interested in:

The Laravel framework implements the example of adding, deleting, modifying and checking the model layer

The ThinkPHP framework implements the method of exporting excel data

Native JS implements Ajax to interact with PHP through POST method example php skills

##

The above is the detailed content of Layui framework implements file upload and TP3.2.3 background processing operation example for uploaded files. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Implement file upload and download in Workerman documents Implement file upload and download in Workerman documents Nov 08, 2023 pm 06:02 PM

To implement file upload and download in Workerman documents, specific code examples are required. Introduction: Workerman is a high-performance PHP asynchronous network communication framework that is simple, efficient, and easy to use. In actual development, file uploading and downloading are common functional requirements. This article will introduce how to use the Workerman framework to implement file uploading and downloading, and give specific code examples. 1. File upload: File upload refers to the operation of transferring files on the local computer to the server. The following is used

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

How to use Laravel to implement file upload and download functions How to use Laravel to implement file upload and download functions Nov 02, 2023 pm 04:36 PM

How to use Laravel to implement file upload and download functions Laravel is a popular PHP Web framework that provides a wealth of functions and tools to make developing Web applications easier and more efficient. One of the commonly used functions is file upload and download. This article will introduce how to use Laravel to implement file upload and download functions, and provide specific code examples. File upload File upload refers to uploading local files to the server for storage. In Laravel we can use file upload

How to use gRPC to implement file upload in Golang? How to use gRPC to implement file upload in Golang? Jun 03, 2024 pm 04:54 PM

How to implement file upload using gRPC? Create supporting service definitions, including request and response messages. On the client, the file to be uploaded is opened and split into chunks, then streamed to the server via a gRPC stream. On the server side, file chunks are received and stored into a file. The server sends a response after the file upload is completed to indicate whether the upload was successful.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

See all articles