Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deal with uploading Files in PHP

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to deal with uploaded files in PHP, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Php is the best language (programmers in other languages don't hit me.) Php has a natural advantage in dealing with interactions, and naturally has powerful functions to handle uploaded files.

Just like submitting general data, uploading files requires a form. Let's create a special form to upload the file.

Uploaded files:

Ok, let's analyze this code snippet.

The above enctype specifies the encoding format of the data when it is sent to the server. It has three values:

The MAX_FILE_SIZE hidden field (in bytes) must be placed before the file input field, and its value is the maximum size of the file. This is a suggestion for browsers, and php will also check this item. However, this obstacle can be bypassed on the browser side, so don't expect to use it to block large files. However, the maximum value of the file is limited by post_max_size= (number) M in php.ini. But it is best to add this project, which can avoid the trouble of users spending time waiting for large files to be uploaded before they find that large files have failed.

After the user submits the document form, the server can accept the data. There is a global variable $_ FILES in PHP to process the file, assuming that the upload field name is userfile (you can change it at will in the field).

$_ FILES ['userfile'] [' name'] the original name of the client file.

The MIME type of the $_ FILES ['userfile'] [' type'] file, which is not checked on the PHP side, so this value does not necessarily exist.

$_ FILES ['userfile'] [' size'] the size of the uploaded file in bytes.

The temporary file name stored on the server side after the $_ FILES ['userfile'] [' tmp_name'] file is uploaded.

$_ FILES ['userfile'] [' error'] and the error code associated with the file upload. If the upload is successful, the value is 0.

After uploading, the file is stored in the server's default temporary directory by default, and the upload_tmp_dir in php.ini is set to a different path.

Let's also talk about a move_uploaded_file () function here:

This function checks and ensures that the file specified by file is legally uploaded (that is, uploaded through PHP's HTTP POST upload mechanism). If the file is legal, move it to the file specified by newloc.

If file is not a legal file upload, no action will occur and move_uploaded_file () will return false.

If file is a legitimate upload file, but cannot be moved for some reason, and no action occurs, move_uploaded_file () returns false with a warning.

This check is particularly important if the uploaded file is likely to cause its content to be displayed to users or other users of the system.

The following is an example of php uploading files:

Upload files processing uploaded files in the upload form:

The above is all the contents of the article "how to deal with uploaded files in PHP". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report