search
Homephp教程php手册PHP study notes: File upload (including setting the file upload size limit)

Today I am writing about file upload. I forgot the correct rules I learned by myself a few days ago. I used a very stupid way to judge the format and directly entered the code:

<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * Created by PhpStorm.
 * User: Administrator
 * Date: 16-9-12
 * Time: 上午10:01
 *
 * 文件上传
 *
 </span><span style="color: #008000;">*/</span>




?>


    
上传文件:
php if(isset($_POST['subbtn'])){ echo $_FILES['file']['type']."
"; /* * JPG image/jpeg * GIF image/gif * PNG image/png * */ //文件上传处理程序: //$_FILES['file']['type'] 上传文件的类型 //$_FILES['file']['size'] 上传文件的大小 //$_FILES['file']['error'] 上传错误代码 //$_FILES['file']['name'] 上传文件名 //$_FILES['file']['tmp_name'] 临时文件名 //$fileName="a.TXT"; //$pos = strrpos($fileName,"."); //$ext = strtolower(substr($fileName,$pos)); // $file = $_FILES['file']; $fileName=$file['name']; echo '后缀切割前:'. $file['name']."
"; //确定最后一个.出现的位置 $pos = strrpos($file['name'],'.'); //转换文件名为小写 $ext = strtolower(" $fileName"); //提取文件名后缀 $fileExten=substr($ext,$pos+2); //判断文件后缀时候符合特定要求,这里设置为:jpg jpeg doc if($fileExten=='jpg'||$fileExten=='jpeg'||$fileExten=='doc' ){ echo "文件后缀:". $fileExten. "
"; echo "文件类型:". $file['type']."
"; echo "文件大小:". $file['size']."
"; echo "错误代码:". $file['error']."
"; echo "文件名:". $file['name']."
"; echo "临时文件名:". $file['tmp_name']."
"; move_uploaded_file($file['tmp_name'], "files/".$file['name']); }else echo "文件格式不对"; } ?>

If the format is incorrect, it will be intercepted directly.

If you want to modify the uploaded file size limit, you can refer to the following methods:

1. General file upload, unless the file is very small. Like a 5M file, it may take more than a minute to upload.
But in php, the default maximum execution time of the page is 30 seconds. That is to say If it exceeds 30 seconds, the script will stop executing.
This will result in the inability to open the web page. At this time, we can modify max_execution_time
Look in php.ini
max_execution_time
The default is 30 seconds. Change to
max_execution_time = 0
0 Indicates no limit
2. Modify post_max_size to set the maximum size allowed for POST data. This setting also affects file uploads.
php default post_max_size is 2M. If the POST data size is greater than post_max_size $_POST and $_FILES superglobals will be empty.
Look for post_max_size. Change to
post_max_size = 150M
3. Many people will change the second step. But upload the file The maximum size is still 8M.
Why? We also need to change a parameter upload_max_filesize to indicate the maximum size of the uploaded file.
Look for upload_max_filesize, the default is 8M and change it to
upload_max_filesize = 100M
In addition, it should be noted that post_max_size is better than upload_max_filesize.

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

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

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools