Home  >  Article  >  PHP Framework  >  How to modify the size of uploaded files in thinkphp

How to modify the size of uploaded files in thinkphp

PHPz
PHPzOriginal
2023-03-31 13:52:271562browse

ThinkPHP is an excellent PHP development framework that provides rich extension functions to facilitate developers to quickly complete various needs. During development, file uploading is a common requirement. But by default, ThinkPHP upload file size is limited. So, how to modify the upload file size?

1. PHP.ini configuration modification

The first thing to note is that there is a limit on the upload file size in the PHP.ini file. Therefore, we can modify the size of the uploaded file by modifying the PHP.ini file. The specific steps are as follows:

  1. Search for the php.ini file on your computer and open it.
  2. Search for the upload_max_filesize and post_max_size parameters and modify their values ​​to the required size. In general, the value of upload_max_filesize should be greater than or equal to the value of post_max_size. For example, if the size of the file we need to upload is 10MB, we can modify the values ​​of upload_max_filesize and post_max_size to 10M.
  3. Save the file and restart Apache to take effect.

2. Modify the application configuration file

Modifying the PHP.ini file is global. If you only want to modify the upload file size in a certain application, you need to modify the ThinkPHP application configuration. document. The specific steps are as follows:

  1. Open the application configuration file config.php.
  2. Search for the upload_max_filesize and post_max_size parameters and modify their values ​​to the required size.
'upload_max_filesize' => '10M',
'post_max_size' => '10M',
  1. Save the file and restart Apache to take effect.

3. Modify the verification rules in the controller

In ThinkPHP, the file type, size, etc. are generally verified when uploading files. We can also modify the controller's Validate rules to modify upload file size. The specific steps are as follows:

  1. Open the controller file that needs to be modified.
  2. Search for verification rules and modify them. For example, if the size of the file to be uploaded is 10MB, you can modify the $fileSize parameter to 10485760 (in bytes):
$validate = new \think\Validate([
    'file' => 'fileSize:10485760|fileExt:xlsx,xls',
]);
  1. Save the file and the uploaded file will take effect.

Summary:

Whether it is by modifying the PHP.ini configuration or modifying the application configuration file or controller verification rules, as long as you master the method of modifying the size of the uploaded file, during the subsequent development process Easily handle various file upload needs. It should be noted that increasing the size of uploaded files will also increase server pressure and risks, so please adjust carefully.

The above is the detailed content of How to modify the size of uploaded files 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