Simple PHP file upload details-PHP source code

Source: Internet
Author: User
Tags form post php file upload
We have talked a lot about File Upload. This is just the most basic simple file upload function, at the same time, this tutorial also introduces the upload principle, usage of various functions, file parameter descriptions, and php. ihi sets the size of the uploaded file. We have talked a lot about File Upload. This is just the most basic simple file upload function, at the same time, this tutorial also introduces the upload principle, usage of various functions, file parameter descriptions, and php. ihi sets the size of the uploaded file.

Script ec (2); script

1. If you upload a small file (2 mb), you do not need to modify the configuration in php tutorial. ini. If you upload a large file, you must modify the configuration.

2. Modify the following content in php. ini:

Post_max_size indicates the maximum value that php can receive through form post.

Upload_max_filesize indicates the maximum value allowed to upload files.

3. Upload form settings

Explanation: 1. The entype attribute must be set to multipart/form-data during upload.

2 It is best to add the hidden domain name value to the form as max_file_size. This hidden domain does not actually limit the size of the uploaded file, but is used to prevent users from mistakenly uploading large files and getting stuck in endless waiting. The actual size limit is the two modifications in php. ini.

4 pre-defined variables $ _ files

First, it is a two-dimensional array,

$ _ Files ['upfile'] ['name'] file name to be uploaded

$ _ Files ['upfile'] ['SIZE'] size of the uploaded file

$ _ Files ['upfile'] ['tmp _ name'] When uploading a file, the file is first saved as a temporary file, and changed to a temporary file name.

$ _ Files ['upfile'] ['type'] File Upload type

$ _ Files ['upfile'] ['error'] error code

Note: $ _ files ['upfile'] ['name'] The upfile here is consistent with the formCorresponding to the name of, do not make a mistake


5. Specific implementation

$ Filename = $ _ files ['upfile'] ['name'];

$ Tmp = $ _ files ['upfile'] ['tmp _ name'];

$ Error = $ _ files ['upfile'] ['error'];

$ Path = "./images /";

If ($ error = 0 ){

If (is_uploaded_file ($ tmp )){

If (move_uploaded_file ($ tmp, $ path. $ filename )){

Echo "Upload successful! ";

} Else {

Echo "script" alert ('file illegal '); history. go (-1); script ";

}


} Else {

Echo "script alert ('invalid operation! '); History. go (-1); script ";

}


} Else {

Echo "script" alert ('upload error, error type: ". $ error." '); history. go (-1); script ";

}

?>

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.