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 upload files in PHP

2025-03-29 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 upload 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!

Configure php.ini

To use the file upload feature, we first need to configure php.ini and set the parameters properly. Let's first take a look at how to find the php.ini file. Examples are as follows:

First of all, we enter:

Output result:

From the above results, the specific path of php.ini can be seen in the Loaded Configuration File column. This leads to the php.ini file.

There are too many files in php.ini. You can use ctrl+F to search for related configuration items. Then the parameters we need to configure are as follows:

File_uploads: on, which means that the server has enabled the file upload feature; if it is off, the server has disabled the file upload feature.

Upload_tmp_dir: the temporary directory where files are uploaded. Before the file is successfully uploaded, the file will first be stored in the temporary directory on the server side, and the default directory of the system will not be set.

Upload_max_filesize: the maximum number of files allowed to be uploaded by the server, in MB.

In max_execution_time:PHP, the maximum time, in seconds, that an instruction can execute.

The memory space, in MB, allocated by an instruction in memory_limit:PHP.

We need to note that if you want the configuration to take effect after the configuration is completed, you need to restart the Apache server before the configured parameters take effect.

Predefined variable $_ FILES

After we have configured php.ini, we need to make some restrictions and judgments on the uploaded file by predefining the variable $_ FILES. The $_ FILES variable stores the information about the uploaded file, and the information it needs to save is as follows:

$_ files [filename] [name]: save the file name of the uploaded file

$_ files [filename] [size]: save the size of the uploaded file

$_ files [filename] [tmp_name]: save the temporary name of the uploaded file

$_ files [filename] [type]: save the type of uploaded file

$_ files [filename] [error]: the code for saving the uploaded file result. 0 indicates success.

We can combine the predefined variable $_ FILES with HTML to do the following:

PHP file upload

After running the program, select the file to upload according to the form. I take test1.txt as an example, and the output is as follows:

According to the saved information, we can get the relevant information about the file:

The type of file named test1.txt; upload file is text/plain;, and the temporary name of saving upload file is C:\ Windows\ phpD16F.tmp;0, which means that the file was uploaded successfully; the size of the file is 5.

Single file upload

Through the above code, we have the basic information about the file to be uploaded, and then we need to use the move_uploaded_file () function to upload. Its main function is to move the file you just uploaded to a new location. Its syntax format is as follows:

Move_uploaded_file (string $filename, string $destination)

Where $filenameb represents the file name of the uploaded file, which is not the original file name of the uploaded file, but the file name obtained from tmp_name in the previous step $_ FILES; $destinationb indicates the location to which the uploaded file is to be moved.

TRUE is returned if the function is executed successfully, and FALSE is returned if it fails.

Let's take a look at the example:

Upload the test1.txt file to a folder called phptest that I created in the root directory as follows:

PHP file upload

After uploading the file according to the form, the output result is as follows:

This means that the test2.txt I want to upload has been uploaded to the directory I need:

Multiple file upload

Through the above example, we have learned the process of single file upload, but what is often used in daily use is multi-file upload, so how to upload multiple files?

Examples are as follows:

Also as in the example above, upload multiple files to the phptest folder I created in the root directory

PHP file upload

Output result:

So the results show that several files have been uploaded to my folder:

The above is all the contents of the article "how to upload 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