search
HomeBackend DevelopmentPHP TutorialTalk about the processing of uploaded files in php, _PHP tutorial

Talk about the processing of uploaded files in php,

This is an era of forms. . .

When we edit our own information in the browser, we will encounter uploading avatars; in the library, we will upload documents... The word "upload" exists everywhere.

PHP is the best language (programmers in other languages, please don’t hit me...). PHP has natural advantages in handling interactions, and naturally has powerful functions to handle uploaded files.

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

<span>1</span> <span><</span><span>form </span><span>enctype</span><span>="multipart/form-data"</span><span> action</span><span>="upload_file.php"</span><span> name</span><span>="upload_form"</span><span> method</span><span>="post"</span><span>></span>
<span>2</span>     <span><!--</span><span>MAX_FILE_SIZE必须在所有的input之前,以后要是想用上传表单,可以在form之后就写隐藏的input</span><span>--></span>
<span>3</span>     <span><</span><span>input </span><span>type</span><span>="hidden"</span><span> name</span><span>="MAX_FILE_SIZE"</span><span> value</span><span>="30000"</span><span>/></span>
<span>4</span> <span>    上传的文件:
</span><span>5</span>     <span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="userfile"</span><span>/></span>
<span>6</span>     <span><</span><span>hr</span><span>/></span>
<span>7</span>     <span><</span><span>input </span><span>type</span><span>="submit"</span><span> name</span><span>="sub_button"</span><span> value</span><span>="上传文件的提交按钮"</span><span>/></span>
<span>8</span> 
<span>9</span> <span></</span><span>form</span><span>></span>

OK, let’s analyze this code snippet.

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

 MAX_FILE_SIZE hidden field (unit: 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, PHP will also check this. This barrier can be bypassed on the browser side, so don't expect to use it to block large files. However, the maximum file size is limited by post_max_size= (number)M in php.ini. But it is better to add this item, which can avoid the trouble of users spending time waiting to upload large files only to find that large file upload failed.

After the user submits the file form, the server can accept the data. There is a global variable $_FILES in PHP to process files. It is assumed that the upload field name is userfile (can be changed at will in the field).

 $_FILES['userfile']['name']                                                                                                                                                                                                          $_FILES['userfile']['name']                               The original name of the client file.

 $_FILES['userfile']['type'] The MIME type of the file. This is not checked on the PHP side, so this value may not exist yet.

$_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'] and the file upload related error code. If the upload is successful, the value is 0.

After the file is uploaded, it is stored in the default temporary directory of the server by default, and the upload_tmp_dir in php.ini is set to another path.

Here we have to talk about a move_uploaded_file() function:

 

This function checks to ensure that the file specified by

file is a valid upload file (i.e. uploaded via 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 uploaded file, no operation will occur and move_uploaded_file() will return false.

If

file is a legitimate uploaded file but cannot be moved for some reason, no action will be taken and move_uploaded_file() will return false and a warning will be issued.

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

The following is an example of uploading files in php:

<span> 1</span> <span><</span><span>b</span><span>></span>上传文件处理<span></</span><span>b</span><span>></span>
<span> 2</span> <span><</span><span>hr</span><span>/></span>
<span> 3</span> <span><?</span><span>php
</span><span> 4</span> <span>if (isset($_FILES['userfile'])) {
</span><span> 5</span> <span>    $uploaddir = 'upload/';
</span><span> 6</span> <span>    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
</span><span> 7</span> <span>    echo '<pre class="brush:php;toolbar:false">';
</span><span> 8</span> <span>    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
</span><span> 9</span> <span>        echo '上传文件成功'.'<br>';
</span><span>10</span> <span>    } else {
</span><span>11</span> <span>        echo '上传文件失败'.'<br>';
</span><span>12</span> <span>    }
</span><span>13</span> <span>    echo '这是上传文件的一些信息:' . '<br>';
</span><span>14</span> <span>    print_r($_FILES);
</span><span>15</span> <span>    echo '<pre class="brush:php;toolbar:false">';
</span><span>16</span> <span>    die();
</span><span>17</span> <span>}
</span><span>18</span> 
<span>19</span> <span>?></span>
<span>20</span> <span><</span><span>b</span><span>></span>上传表单<span></</span><span>b</span><span>></span>
<span>21</span> <span><!--</span><span>表单中的enctype属,必须和以下定义保持一致</span><span>--></span>
<span>22</span> <span><</span><span>form </span><span>enctype</span><span>="multipart/form-data"</span><span> action</span><span>="upload_file.php"</span><span> name</span><span>="upload_form"</span><span> method</span><span>="post"</span><span>></span>
<span>23</span>     <span><!--</span><span>MAX_FILE_SIZE必须在所有的input之前,以后要是想用上传表单,可以在form之后就写隐藏的input</span><span>--></span>
<span>24</span>     <span><</span><span>input </span><span>type</span><span>="hidden"</span><span> name</span><span>="MAX_FILE_SIZE"</span><span> value</span><span>="30000"</span><span>/></span>
<span>25</span> <span>    上传的文件:
</span><span>26</span>     <span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="userfile"</span><span>/></span>
<span>27</span>     <span><</span><span>hr</span><span>/></span>
<span>28</span>     <span><</span><span>input </span><span>type</span><span>="submit"</span><span> name</span><span>="sub_button"</span><span> value</span><span>="上传文件的提交按钮"</span><span>/></span>
<span>29</span> <span></</span><span>form</span><span>><br /></span>

http://www.bkjia.com/PHPjc/1129635.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1129635.htmlTechArticleTalk about the processing of uploaded files in php. This is an era of forms. . . When we edit our own information in the browser, we will upload avatars; in the library, we will upload documents....
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

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 and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

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.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft