PHP file upload source code analysis (RFC1867)_PHP tutorial
PHP file upload source code analysis (RFC1867) Friends who need to know can refer to
. HTTP-based upload is relatively much easier to use and safer than FTP. The upload methods that can be applied include PUT , WEBDAV, and RFC1867. This article will analyze how to implement file upload based on RFC1867 in PHP.
RFC1867
RCF1867 is the Form-based File Upload in HTML standard protocol, RFC1867 The standard makes two changes to HTML:
1 adds a file option to the type attribute of the input element.
2 The input tag can have an accept attribute, which can specify a list of file types or file formats that can be uploaded.
In addition, this standard also defines a new mime type: multipart/form-data, and when processing a file with enctype="multipart/form-data" and/or containing < The behavior that should be taken when entering a form marked with input type="file">.
For example, when HTML wants the user to upload one or more files, he can write:
The code is as follows | Copy code | ||||||||
|
The code is as follows | Copy code | ||||
|
The code is as follows | Copy code |
//Request headerPOST /upload.php HTTP/1.0rn.. .Host: www.laruence.comrn...Content-length: xxxxxrn...Content-type: multipart/form-data, boundary=---- ----------7d51863950254rn...rnrn//Start POST data content---------------7d51863950254content -disposition: form-data; name="description"laruence's personal introduction---------------7d51863950254content-disposition: form-data; name ="userfile"; filename="laruence.txt"Content-Type: text/plain... Content of laruence.txt...----------- ----7d51863950254 |
The next step is the server, how to process this data.
Accept upload
As the Web server, it is assumed to be Apache (also assume that PHP is installed in Apache as a module ), when receiving the user's data, first it determines the MIME TYPE as the PHP type based on the HTTP request header, and then after some processes (for this part, please refer to my previous PHP Life Cycle ppt), it will eventually control The power is handed over to the PHP module.
At this time, PHP will call sapi_activate to initialize a request. In this process, it first determines the request type, which is POST at this time, and then calls sapi_read_post_data. Through Content-type, find The processing function rfc1867_post_handler of rfc1867 calls this handler to analyze the data from POST.
The source code of rfc1867_post_handler can be found in mian/rfc1867.c. You can also refer to my previous in-depth understanding. PHP file upload, which also lists the source code.
Then, PHP passes the boundary, and for each segment, it checks whether the:
name and filename attributes are both defined. (Famous file upload)
If name is not defined, filename is defined (unnamed upload)
If name is defined, filename is not defined (ordinary data),
to perform different processing.
The code is as follows | Copy code | ||||||||
char *pair=NULL; int end=0;
|
The code is as follows | Copy code |
/* Normal form variable, safe to read all data into memory */if (!filename && param) { unsigned int value_len; char *value = multipart_buffer_read_body(mbuff, &value_len TSRMLS_CC); unsigned int new_val_len; /* Dummy variable */… if (! strcasecmp(param, "MAX_FILE_SIZE")) { max_file_size = atol(value); >} |
If yes, it will check whether the file size is exceeded according to its value.
The code is as follows | Copy code | ||||
} else if (max_file_size && (total_bytes > max_file_size)) { #if DEBUG_FILE_UPLOAD sapi_module.sapi_error(E_NOTICE, "MAX_FILE_SIZE of % ld bytes exceeded - file [%s=%s] not saved", max_file_size, param, filename); #endif cancel_upload = UPLOAD_ERROR_B; } |
代码如下 | 复制代码 |
if (!skip_upload) {
|
By judging the name and filename, if it is a file upload, it will be based on PHP's Settings, create a temporary file with a random name in the file upload directory:
The code is as follows | Copy code |
if (!skip_upload) {
|
Return the file handle, and the temporary random file name.
代码如下 | 复制代码 |
else if (blen > 0) {
|
代码如下 | 复制代码 |
zend_hash_add(SG(rfc1867_uploaded_files), temp_filename,
|
.....
The code is as follows | Copy code | ||||
wlen = write(fd , buff, blen); //Write temporary file. if (wlen == -1) { /* write failed */ #if DEBUG_FILE_UPLOAD sapi_module.sapi_error (E_NOTICE, "write() failed - %s", strerror(errno)); #endif cancel_upload = UPLOAD_ERROR_F; } } ... . |
The code is as follows | Copy code td> |
zend_hash_add(SG(rfc1867_uploaded_files), temp_filename, strlen(temp_filename) + 1, &temp_filename, sizeof(char *), NULL); |
The code is as follows | Copy code |
$_FILES['userfile'] //name="userfile" |
If it is an unnamed upload, tmp_name will be used to set:
The code is as follows
|
Copy code
|
||||
$_FILES['tmp_name'] //Unnamed upload
|
At this time, in upload.php, the user can operate the file just generated through move_uploaded_file
http: //www.bkjia.com/PHPjc/444674.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
