Home  >  Article  >  PHP Framework  >  Detailed explanation of how to implement file upload function in thinkphp

Detailed explanation of how to implement file upload function in thinkphp

PHPz
PHPzOriginal
2023-04-07 09:32:551916browse

ThinkPHP is an open source web application framework based on the PHP language. Its advantage is that the code is simple and easy to use, while it has powerful functions and efficient performance. In the process of using the ThinkPHP framework, file upload is an operation that is often faced. Therefore, the following will introduce in detail how to use ThinkPHP to implement the file upload function.

1. Preliminary preparation

Before starting project development, we need to carry out some preliminary preparation work. The specific steps are as follows:

  1. Install the ThinkPHP framework: Download the latest version of the ThinkPHP framework from the official website and extract it to the local development environment;
  2. Create the project: In the local Create a new ThinkPHP project in the web server and complete the basic configuration of the project;
  3. Create a directory for uploading files: In the root directory of the project, create a directory for storing uploaded files, for example: Public/ uploads.

2. Write the code for uploading files

  1. Create a form for uploading files

Before writing the code for uploading files, we need to first Create a form for uploading files. The code is as follows:

<form action="{:U(&#39;upload&#39;)}" method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <button type="submit">上传文件</button>
</form>

In the above code, we use the form tag to create the form and set the submission address and submission method of the form. Among them, the value of the enctype attribute is set to multipart/form-data, indicating that the form is used to upload files. The form contains a file selection box and a submit button.

  1. Write the controller code for uploading files

After the form is submitted, you need to write a controller to handle the operation of uploading files. In the ThinkPHP framework, the controller file is located in the Application/Module/Controller directory, so we need to create a file named UploadController.class.php in this directory and write the code to upload the file in the file. The code is as follows:

<?php
namespace Home\Controller;
use Think\Controller;

class UploadController extends Controller {

    public function upload() {
        $upload = new \Think\Upload();
        $upload->maxSize   =     3145728 ;
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');
        $upload->rootPath  =     './Public/uploads/';
        $upload->savePath  =     '';
        $upload->saveName  =     time().rand(1000,9999);
        $info   =   $upload->upload();

        if(!$info) {
            $this->error($upload->getError());
        }else{
            $this->success('上传成功!');
        }
    }
}

In the above code, we created a method named upload in the UploadController class to handle the operation of uploading files. In this method, we first instantiate an upload class \Think\Upload, and set the maximum limit of uploaded files, the file types allowed to be uploaded, the root directory of the uploaded file, the save path of the uploaded file, and the save name of the uploaded file. . Then, we call the upload() method to upload the file. If the file upload fails, the getError() method will be called to obtain the error information of the uploaded file and output. If the file is uploaded successfully, a prompt message of "Upload Successful" will be output.

3. Effect Preview

After the code to upload the file is written, we open the browser and access the upload method of the upload controller to upload the file. After the upload is successful, the file will be saved in the Public/uploads directory. We can verify whether the file is successfully uploaded by accessing the files in this directory.

4. Summary

Through the above detailed introduction, I believe that everyone has mastered the routine operations of uploading files using the ThinkPHP framework. In project development, file upload is a relatively important function. In order to facilitate developers' use, the ThinkPHP framework provides a powerful file upload function, which can greatly improve development efficiency and development experience.

The above is the detailed content of Detailed explanation of how to implement file upload function in thinkphp. 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