Home Backend Development PHP Tutorial In-depth analysis and understanding of PHP file upload principles

In-depth analysis and understanding of PHP file upload principles

Jul 25, 2016 am 09:00 AM

I will give you an in-depth introduction to the relevant principles of PHP file upload. Friends in need can refer to it.

Many PHP tutorials will introduce file upload. As we all know, PHP file upload is simple and efficient. Today I will analyze its principle for you.

//Use multipart/form-data encoding format $_FILES system function; $_FILES['myFile']['name']File name $_FILES['myFile']['type'] file type, restricted by the server image/** image/x-png application/x-zip-compressed $_FILES['myFile']['size']Upload file size $_FILES['myFile']['tmp_name'] saves the temporary file name after uploading the service $_FILES['myFile']['error'] error code; 0 Success 1 Exceeded php.ini size 2 Exceeded the value specified by the MAX_FILE_SIZE option 3Only partial upload 5Uploaded file size is 0

move_uploaded_file (temporary file, target location and file name); Function to move files to the target location after uploading is_uploaded_file(MIME);

Example of determining upload MIME type: 1. html part

<form enctyoe="multipart/form-data" method="post" name="upload">
<input name="upfile" name="name">
</form>
Copy after login

2. File upload code

<?php
if(is_uploaded_file($_FILES['myFile']['tmp_name'])){
$upfile = $_FILES['upload'];
$name = $upfile['name'];
$type = $upfile['type'];
$size = $upfile['size'];
$tmp_name = $upfile['tmp_name'];
$error = $upfile['error'];
switch($type){
case 'image/pjpeg' : $ok=1;
break
}
if($ok){
move_uploaded_file($tmp_name,'up/'.$name);
}else{
echo "不允许的文件类型";
}
}
?>
Copy after login

--------------------------------------------- The principle and implementation of PHP file upload 1. Form 1. The form for uploading files uses the post method (needless to say the difference from get); also add enctype='multipart/form-data'. 2. Generally, a hidden field should be added: , located in front of the file field. The value of value is the client byte limit for uploading files. It is said that it can reduce the waiting time of the client when the file exceeds the standard, but I don't think there is any difference. 3. For security reasons, value assignment is not allowed in the file field. Just enter a string in the file field and press submit, but nothing will happen. Submit must agree to "service" only when the second character is a colon (for example, a space followed by a colon can upload a "file" with a length of 0 bytes) - but this is a client-side measure, and it is easy to circumvent like MAX_FILE_SIZE. past.

Second, file upload error code Copy a paragraph first: The predefined variable $_FILES array has 5 contents: $_FILES['userfile']['name'] - the original name of the client machine file $_FILES['userfile']['type'] - MIME type of file $_FILES['userfile']['size']——The size of the uploaded file, in bytes $_FILES['userfile']['tmp_name']——The temporary file name stored on the server after the file is uploaded $_FILES['userfile']['error']——Error code related to the file upload

Among them, $_FILES['userfile']['error'] can have the following values ​​and meanings: 0 - No errors occurred and the file was uploaded successfully. 1 - The uploaded file exceeds the limit of the upload_max_filesize option in php.ini. 2 - The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. 3 - Only part of the file was uploaded. 4 - No files were uploaded. Needless to say 1~3. "No file was uploaded" (4) means that the file field of the form has no content and is an empty string. "File uploaded successfully" (0) does not necessarily mean that a file has actually been uploaded. For example, if you type "c:" in the file field, you can see "Upload successful" - the error code is 0, ['name'] is "c:", ['type'] is "application/octet-stream" , ['size'] is 0, ['tmp_name'] is "xxx.tmp" (xxx is the name given by the server) 3. File size limits and inspections Factors that limit the size of uploaded files include 1. The value of the client’s hidden field MAX_FILE_SIZE (can be bypassed). 2. Server-side upload_max_filesize, post_max_size and memory_limit. These items cannot be set using scripts. 3. Customize file size limit logic. Even if you can decide the server's limits yourself, there may be situations that require individual consideration. So this limiting method is often necessary. A situation I encountered may not be universal, let me explain. If the file is much larger than the server-side limit (upload_max_filesize), but has not reached or is close to post_max_size or memory_limit, $_FILES will "collapse" - the result is that $_FILES['userfile'] becomes "Undefined index", of course There is no test that can be done.

Server-side restriction checking takes precedence over client-side restriction checking. That is to say, if the two limits are the same and the file is too large, $_FILES['userfile']['error'] will issue error code 1. Only when the client limit is smaller than the server limit to a certain "degree" and the file size exceeds both, error code 2 will appear (could this be the reason why I feel that MAX_FILE_SIZE does not play the expected role?). The above "level" was tested on my machine between 3 and 4K - the server limit set on my machine is 2M... Because it doesn't make sense, there is no precise rule.

When error code 1 or 2 appears: $_FILES['userfile']['name'] is the original name of the client machine file $_FILES['userfile']['type'] is an empty string $_FILES['userfile']['size'] is 0 $_FILES['userfile']['tmp_name'] is an empty string

四,文件路径检验 file域无输入,错误代码为4(无文件上传) $_FILES['userfile']['name']为空字符串 $_FILES['userfile']['type']为空字符串 $_FILES['userfile']['size']为0 $_FILES['userfile']['tmp_name']为空字符串 file域是非文件路径的字符串(不考虑客户端的假“限制”了),错误代码是0(“上传成功”) $_FILES['userfile']['name']为原字符串 $_FILES['userfile']['type']为application/octet-stream $_FILES['userfile']['size']为0 $_FILES['userfile']['tmp_name']为一个暂时文件名

五,is_uploaded_file()的返回值 手册上面不很详细地说,用法是: bool is_uploaded_file( string filename) 实际上 is_uploaded_file($_FILES['userfile']['name']); 总是返回FALSE。后来看见别人是用: is_uploaded_file($_FILES['userfile']['tmp_name']);

比较一下: file域无输入——————返回FALSE——error=>4,name=>'', tmp_name=>'', type=>'', size=>0 file域为非路径字符串——返回 TRUE——error=>0,name=>'xxx',tmp_name=>'yyy',type=>'zzz',size=>0 文件上传成功——————返回 TRUE——error=>0,name=>'xxx',tmp_name=>'yyy',type=>'zzz',size=>sss 文件太大————————返回FALSE——error=>1,name=>'xxx',tmp_name=>'', type=>'', size=>0 文件太大————————返回FALSE——error=>2,name=>'xxx',tmp_name=>'', type=>'', size=>0 文件部分上传——————没机会试验 —error=>3

有点怀疑这个函数是怎么工作的,还是觉得用$_FILES['userfile']['size']检验好些。

六,检验顺序 if($_FILES['userfile']['error']!=4){//有文件上传 if($_FILES['userfile']['error']!=3){//全部上传了 if($_FILES['userfile']['error']!=1){//不超过服务器端文件大小限制 if($_FILES['userfile']['error']!=2){//不超过客户端文件大小限制 if($_FILES['userfile']['size']>0){//确实是文件 if(......){//自定义文件大小检验逻辑 if(......){//自定义文件类型检验逻辑 if(move_uploaded_file($_FILES['userfile']['tmp_name'],...))//移动文件 //.......... } else give_a_message(...); } else give_a_message(...); } else give_a_message(...); } else give_a_message(...); } else give_a_message(...); } else give_a_message(...); } else give_a_message(...); } 附代码: ------------------------------ 1)、test.php:

<html>
<body>
 <form enctype="multipart/form-data" action="upload.php" method="POST">
     <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
     Send this file: <input name="userfile" type="file" accept="image/x-png,image/gif,image/jpeg"/>
     <input type="submit" value="Send File" />
 </form>
</body>
</html>
Copy after login

2)、upload.php

<html>
<body>
 <?php
  $uploaddir = 'images/';
  $uploadfile = $uploaddir. $_FILES['userfile']['name'];
  print "<pre class="brush:php;toolbar:false">";
  if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
     print "File is valid, and was successfully uploaded.  Here's some more debugging info:\n";
     print_r($_FILES);
  } else {
     print "Possible file upload attack!  Here's some debugging info:\n";
     print_r($_FILES);
  }
  print "
Copy after login
"; ?>


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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles