Some questions about uploading files in PHP

Source: Internet
Author: User
Tags http post
This article mainly introduces the interpretation of PHP upload file processing problems related data, very good, with reference value, the need for friends can refer to the next

We edit our own information in the browser, we will encounter the upload picture, in the library, we will upload the document ... The word "upload" exists everywhere.

PHP is the best language (Other language program apes don't hit me ... )。 PHP has a natural advantage in dealing with interactions, and naturally has powerful functions to handle uploading files.

Like submitting general data, uploading a file also requires a form. Create a special form below to upload the file.

<form enctype= "Multipart/form-data" action= "upload_file.php" name= "Upload_form" method= "POST" ><!--MAX_ File_size must be before all input, and later if you want to use the upload form, you can write the hidden input--><input after the form type= "hidden" name= "max_file_size" value= "/ > uploaded files: <input type= "file" Name= "UserFile"/><input type= "Submit" Name= "Sub_button" value= "submit button for upload file"/ ></form>

OK, let's analyze This code snippet.

The enctype above specifies what encoding format the data is used for when it is sent to the server. It has a value of three:

Max_file_size hidden Fields (in bytes) must precede the file input field with a value that is the maximum size of the file. This is a suggestion for the browser, PHP will also check this. However, you can bypass this barrier on the browser side, so don't expect to use it to block large files. However, the maximum file size is limited by the post_max_size= (number) m in php.ini . But the best thing to do is add the project, which avoids the hassle of having to wait for a large file to be uploaded before the user discovers a large file upload failure.

After the user submits the file form, the server side can accept the data. PHP has a global variable $_files to process the file, assuming that the upload field name is UserFile (can be changed in the field arbitrarily).

$_files[' UserFile ' [' Name '] The original name of the client file.
The MIME type of the $_files[' userfile ' [' type '] file is not checked on the PHP side, so this value is not necessarily available.
$_files[' userfile ' [' size '] the size (in bytes) of the uploaded file.
$_files[' UserFile ' [' tmp_name '] files are uploaded on the server side after the temporary file name is stored.
$_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 to the server default temp directory by default, and the Upload_tmp_dir in PHP.ini is set to a different path.

Here you have to say a move_uploaded_file () function:

This function checks and ensures that files specified by file are valid upload files (i.e. uploaded via PHP's HTTP POST upload mechanism). If the file is valid, move it to the file specified by Newloc.

If file is not legitimately uploaded, no action will occur and Move_uploaded_file () will return false.

If file is a legitimate upload, but for some reason it cannot be moved, nothing happens, Move_uploaded_file () returns false, and a warning is issued.

This check is especially important if the uploaded file is likely to cause the user or other users of the system to display its content.

Here is an example of a PHP upload file:

<b> upload file Processing </b><?phpif (isset ($_files[' UserFile ')) {$uploaddir = ' upload/'; $uploadfile = $uploaddir. basename ($_files[' userfile ' [' name ']); Echo ' <pre> '; if (Move_uploaded_file ($_files[' userfile ' [' Tmp_name '], $uploadfile) {echo ' upload file succeeded '. ' <br> ';} else {echo ' failed to upload file '. ' <br> ';} Echo ' This is some information about uploading a file: '. ' <br> ';p rint_r ($_files); Echo ' <pre> ';d ie ();}? ><b> Upload Form </b><!--the enctype genus in the form must be consistent with the following definition--><form enctype= "multipart/form-data" action= " upload_file.php "Name=" Upload_form "method=" POST "><!--max_file_size must be before all input, and later if you want to upload a form, Can be written after the form of hidden input--><input type= "hidden" name= "max_file_size" value= ""/> uploaded files: <input type= "File" name = "UserFile"/>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.