Further explanation of file upload process with PHP

Source: Internet
Author: User

Uploading files to the server is a technique that we often use when doing projects. Do know that ASP development, ASP upload file is very resistant to, at least need to borrow a group of other means, such as their own VB code, the use of free upload components, can be described as suffering the extreme. Fortunately, PHP developers write the upload function for us, so that PHP can easily upload text files, or even binary files. So in the development of the ASP's comrades, quickly switch to our PHP development bar, hehe ~ ~

First, upload a single file

You can create a special form to support file uploads as follows:

File Upload Form

<form enctype="multipart/form-data" action="URL" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000">
<input name="myFile" type="file">
<input type="submit" value="上传文件">
</form>

The URL here should point to a PHP file that handles uploading files. Max_file_size The hidden field must precede the file input field and its value is the maximum size of the accepted file, in bytes. Note that this value actually does not really control the size of the upload file on the client.

The file information generated by the upload action is stored in a specific array, and the name of the array differs according to the PHP version and configuration file settings. The global variable _files array has already been supported from the PHP4.1.0 version. The $HTTP _post_files array is supported from PHP4.0.0. However, it is recommended that you use the _files array because the associated variable name can also be used when the register_globals is set to off in PHP.ini, and it has been set to off since the PHP4.2.0 version. Nor do we advocate that you change to on.

After the form is added Phpinfo (), the function, the form is submitted, and you see the contents of the _files array:

$_files[the original name of the ' MyFile ' [' name '] client file, same as on $myFile _name.

The MIME type of the $_files[' myFile ' [' type '] file requires the browser to provide support for that information, such as "Image/gif".

$_files[' myFile ' [' size '] the size of the uploaded file, in bytes.

$_files[' myFile ' [' tmp_name '] files are stored in the server after the temporary file name, typically the system default. Can be specified in the php.ini upload_tmp_dir, but the putenv () function setting does not work.

$_files[' myFile ' [' Error '] and the file upload related error code. [' ERROR '] is added in PHP version 4.2.0. Here's what it says: (They're constant after PHP3.0)

Upload_err_ok

Value: 0; No error occurred, file upload succeeded.

Upload_err_ini_size

Value: 1; The uploaded file exceeds the value of the Upload_max_filesize option limit in php.ini.

Upload_err_form_size

Value: 2; The size of the upload file exceeds the value specified by the Max_file_size option in the HTML form.

Upload_err_partial

Value: 3; The file is only partially uploaded.

Upload_err_no_file

Value: 4; No files were uploaded.

Value: 5; The upload file size is 0.

When the file is uploaded, it is stored in the temporary directory by default, and you must remove it from the temporary directory or move it to another location, if not, it will be deleted. That is, whether or not the upload succeeds, the files in the temp directory will be deleted after the script is executed. So before you delete it, copy it to another location using PHP copy (), which completes our upload file process.

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.