PHP Upload principle and Operation implementation. PHP file Upload principles and operations to implement function class libraries for PHP file upload. There are many well-encapsulated libraries on the Internet, which can be used directly. This article only describes the upload principle and simple PHP Upload principle and Operation implementation.
There are many well-encapsulated function libraries for PHP file upload. you can use them directly.
This article only talks about the upload principle and simple Upload operations, and the old bird ignores the HA _ ^ ~
There are also some security judgments, such as: the server restricts the ability to receive image files, while the client maliciously changes the extension of the virus file to an image matching file for upload.
(For example, if a single file is uploaded, the principle of multiple files remains the same, but there are more tips)
Index.html
Upload files
1. Form label enctype attributes
In the form, enctype = "multipart/form-data" is used to set the MIME encoding of the form.
By default, the encoding format is application/x-www-form-urlencoded and cannot be used for file upload;
Only when multipart/form-data is used and the submission method is Post can the complete file data be transmitted.
2. MAX_FILE_SIZE hidden field
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. (But in view of the friendliness, it is best to add this item to the form, because it can avoid the trouble of uploading a large file after you wait for the upload of a large file .)
Upload. php
Print_r ($ _ FILES );
?>
We can see that:
Array
(
[File] => Array
(
[Name] => Photo File .jpg
[Type] => image/jpeg
[Tmp_name] => F: \ wamp \ tmp \ php41BB. tmp
[Error] => 0
[Size] = & gt; 73886
)
)
3. application of global variable $ _ FILES
$ _ FILES ['file'] ['name'] indicates the original file name to be uploaded.
$ _ FILES ['file'] ['type'] indicates the MIME type of the uploaded file.
$ _ FILES ['file'] ['size'] size of the uploaded file, in bytes
$ _ FILES ['file'] ['tmp _ name'] temporary file name stored on the server after the file is uploaded ()
$ _ FILES ['file'] ['error'] error code for file Upload
4. by default, the uploaded file is saved in the temporary folder on the server, and its directory is set in php. ini.
Some common settings related to file upload in php. ini:
File_uploads; specifies whether to enable file upload over HTTP. ON is enabled by default.
Upload_tmp_dir; upload the file to the place where the temporary file is stored on the server. if it is not specified, the default temporary folder will be used.
Upload_max_filesize; that is, the maximum file size that can be uploaded. The default value is 2 MB.
Post_max_size refers to the maximum value that can be received by the form POST to PHP, including all values in the form. The default value is 8 MB.
The following is the complete code for uploading a single file, because it is just as you want to write, it may be a bit messy with logical nesting, understanding the principle is the most important.
// Obtain the information of the uploaded file
$ FileName = $ _ FILES ['file'] ['name'];
$ FileType = $ _ FILES ['file'] ['type'];
$ FileError = $ _ FILES ['file'] ['error'];
$ FileSize = $ _ FILES ['file'] ['size'];
$ TempName = $ _ FILES ['file'] ['tmp _ name']; // temporary file name
// Define the Upload file type
$ TypeList = array ("image/jpeg", "image/jpg", "image/png", "image/gif"); // defines the allowed types
If (! Is_uploaded_file ($ tempName )){
// Determine whether the object is uploaded by POST.
Exit ("not uploaded via http post ");
} Else {
If (! In_array ($ fileType, $ typeList )){
Exit ("the uploaded file is not of the specified type ");
} Else {
If (! Getimagesize ($ tempName )){
// Prevent users from uploading malicious files. for example, you can change the virus file extension to the image format.
Exit ("the uploaded file is not an image ");
}
}
If ($ fileError> 0 ){
// File upload error code determination
Switch ($ fileError ){
Case 1:
$ Message = "the uploaded file exceeds the limit of the upload_max_filesize option in php. ini. ";
Break;
Case 2:
$ Message = "the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. ";
Break;
Case 3:
$ Message = "only part of the file is uploaded. ";
Break;
Case 4:
$ Message = "no file is uploaded. ";
Break;
Case 6:
$ Message = "The temporary folder cannot be found. ";
Break;
Case 7:
$ Message = "file writing failed ";
Break;
Case 8:
$ Message = "file Upload interrupted due to PHP extensions ";
Break;
}
Exit ("file Upload failed:". $ fileError );
} Else {
'If ($ fileSize> 100000 ){
// Limit the size of the file to be uploaded for a specific form
Exit ("The size of the uploaded file exceeds the limit ");
} Else {
// Avoid garbled Chinese names of uploaded files
$ FileName = iconv ("UTF-8", "GBK", $ fileName); // Convert the character encoding captured by iconv from UTF-8 to gbk output
If (move_uploaded_file ($ tempName, "uploads/". $ fileName )){
Echo "file uploaded! ";
} Else {
Echo "failed to upload file ";
}
}
}
}
?>
5. some common functions for php file uploading: (I will not post them for specific usage. check the API documentation)
File_exists check whether the file or directory exists
Is_uploaded_file: checks whether the file is uploaded through http post.
Move_uploaded_file: move the uploaded file to a new location.
Is_writable: determines whether a given file name can be written.
Iconv Character encoding
Getimagesize check whether it is an image file (other types of files can be detected even if the suffix is changed)
The functions and libraries for PHP file uploading are well encapsulated on the Internet. you can use them directly. This article only describes the upload principles and conciseness...