PHP file upload_php tutorial

Source: Internet
Author: User
PHP file upload. In PHP, file upload is generally implemented through move_uploaded_file. Boolmove_uploaded_file (stringfilename, stringdestination) this function checks and ensures that the file specified by filename is in PHP. file upload is generally implemented through move_uploaded_file.
Bool move_uploaded_file (string filename, string destination) this function checks and ensures that the file specified by filename is a valid Upload File (that is, the file uploaded through the http post Upload mechanism of PHP ). If the file is valid, move it to the file specified by destination.

If filename is not a valid file to be uploaded, no operation is performed. move_uploaded_file () returns FALSE.

If filename is valid but cannot be moved for some reason, no operation will occur. move_uploaded_file () returns FALSE. A warning is also issued.

The following example shows how PHP uploads files.
Original file of test. php

The code is as follows:




Website file Upload instance






MAX_FILE_SIZE the hidden field (in bytes) must be placed before the input field of the file, and its value is the maximum size of the received file. This is a suggestion for the browser. PHP will also check this item. This setting can be simply bypassed on the browser side, so do not expect this feature to block large files. In fact, the maximum value of uploaded files in PHP settings will not expire. However, it is best to add this project to the form, because it can avoid the trouble of file Upload failure only after the user spends the time waiting to upload a large file.

Note: Make sure that the attribute of the file upload form is enctype = "multipart/form-data". Otherwise, the file cannot be uploaded.

On the upload page, the information of the uploaded file is obtained through the array $ _ FILES. Assume that the name of the file upload field is "upfile" as shown in the preceding example. the file information is:

$ _ FILES ['upfile'] ['name']

The original file name of the uploaded file.

$ _ FILES ['upfile'] ['type']

The MIME type of the file, if the browser provides this information. An example is "image/gif ". However, this MIME type is not checked on the PHP end, so do not take it for granted.

$ _ FILES ['upfile'] ['size']

The size of the uploaded file, in bytes.

$ _ FILES ['upfile'] ['tmp _ name']

Temporary file name stored on the server after the file is uploaded.

$ _ FILES ['upfile'] ['error']

The error code related to the file upload.

PHP returns an error code along with the array of file information. This code can be found in the error field in the file array generated during File upload. The code is as follows:

UPLOAD_ERR_ OK

The value is 0, and the file is uploaded successfully.

UPLOAD_ERR_INI_SIZE

The value is 1. the uploaded file exceeds the limit of the upload_max_filesize option in php. ini. the default value is 2 MB. If you want to upload a larger file, you can search for upload_max_filesize = 2 M in php. ini to make the file take effect.

UPLOAD_ERR_FORM_SIZE

The value is 2, and the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.

UPLOAD_ERR_PARTIAL

The value is 3, the file is not completely uploaded, and only part of the file is uploaded.

UPLOAD_ERR_NO_FILE

The value is 4, and the file upload fails.

UPLOAD_ERR_NO_TMP_DIR

The value is 6 and the temporary folder cannot be found. PHP 4.3.10 and PHP 5.0.3 are introduced.

UPLOAD_ERR_CANT_WRITE

If the value is 7, the file fails to be written. PHP 5.1.0 is introduced.

Note: the above value is changed to a PHP constant after PHP 4.3.0.
Original Code of action. php
/**
Welcome to our site to share your learning experience

*/

Function getname ($ exname ){
$ Dir = "tmp /";
$ I = 1;
If (! Is_dir ($ dir )){
Mkdir ($ dir, 0777 );
}

While (true ){
If (! Is_file ($ dir. $ I. ".". $ exname )){
$ Name = $ I. ".". $ exname;
Break;
}
$ I ++;
}

Return $ dir. $ name;
}

$ Exname = strtolower (substr ($ _ FILES ['upfile'] ['name'], (strrpos ($ _ FILES ['upfile'] ['name'], '. ') + 1 )));
$ Uploadfile = getname ($ exname );

If (move_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name'], $ uploadfile )){
Echo "file uploaded successfully!

";
} Else {
Echo "file Upload failed!

";
}
Echo:


Original file name: ". $ _ FILES ['upfile'] ['name'].
"

Type: ". $ _ FILES ['upfile'] ['type'].
"

Temporary File name: ". $ _ FILES ['upfile'] ['tmp _ name'].
"

File size: ". $ _ FILES ['upfile'] ['size'].
"

Error code: ". $ _ FILES ['upfile'] ['error'];
?>

Make sure that the permissions on the folder where the uploaded files are stored are 777. This is especially evident on the server. sometimes the upload code we write has no errors, however, the upload function cannot be implemented on the Internet, which is the reason.

This article only describes how to use PHP to upload files. if you have not read this part, you can refer to it.

Implemented by terminate. Boolmove_uploaded_file (stringfilename, stringdestination) this function checks and ensures that files specified by filename...

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.