Home  >  Article  >  Backend Development  >  PHP upload principle and implementation

PHP upload principle and implementation

巴扎黑
巴扎黑Original
2016-11-11 14:08:381058browse

About the uploading principle and simple uploading operation:


                                                                                                                                          to /form-data" is used to set The MIME encoding of the form. By default, this encoding format is application/x-www-form-urlencoded, which cannot be used for file upload;
Only when multipart/form-data is used and the submission method is Post, the file data can be completely transferred.
2. MAX_FILE_SIZE hidden field
MAX_FILE_SIZE hidden field (unit: bytes) must be placed before the file input field, and its value is the maximum size of the received file. This is a recommendation for browsers, PHP will also check this.
This setting can be easily bypassed on the browser side, so don’t expect to use this feature to block large files. (However, in view of friendliness, it is better to add this item to the form, because it can avoid the trouble of users spending time waiting to upload large files only to find that the file is too large and the upload failed.)



//upload.php print_r($_FILES);?>Array(
[file] => Array
                                                                                             [tmp_name] => F:wamptmpphp41BB. tmp
                                                                                                                                                                              'name']; // It is the original file name of the uploaded file
$_FILES['file']['type']; //It is the MIME type of the uploaded file
$_FILES['file']['size']; //The size of the uploaded file, The unit is bytes
$_FILES['file']['tmp_name']; //The 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 will be saved in a 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 ; //Switch whether to allow file upload via HTTP. The default is ON, which means upload_tmp_dir is turned on; //Files are uploaded to the place where temporary files are stored on the server. If not specified, the system default temporary folder upload_max_filesize will be used; //That is, the maximum size of files allowed to be uploaded. The default is 2Mpost_max_size; //Refers to the maximum value that can be received through form POST to PHP, including all values ​​​​in the form. Default is 8M



Simple code:



//Get uploaded file information
$fileName=$_FILES['file']['name']; $fileType=$_FILES['file']['type']; $fileError=$_FILES['file ']['error']; $fileSize=$_FILES['file']['size']; $tempName=$_FILES['file']['tmp_name'];//Temporary file name

//Definition Upload file type
$typeList = array("image/jpeg","image/jpg","image/png","image/gif"); //Define the allowed types

if($fileError>0){ //Upload file error number judgment
                                                                                                                                          use   use   use       use using ’ ’ s ’ ’s ’ s ‐   ‐ ‐ ‐ ​ ​ ​                                                break; case 2: $message="Upload file The size of the file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. break; case 4: $message="No files uploaded."; break; case 6: $message="Temp folder not found."; break; case 7: $message sage="File writing failed";
                            break; File uploaded";                                 break; It’s a file uploaded by POST
exit("No Uploaded via HTTP POST");
}else{ if(!in_array($fileType, $typeList)){ exit("The uploaded file is not of the specified type");
}else{ if(!getimagesize($tempName) ) {// Avoid user uploading malware, such as changing the virus file extension to the picture format
Exit ("The file uploaded is not a picture"); The upload file limits the size
EXIT ("upload files exceed the limit size");
} else {// avoid the Chinese name garbled of upload files
$ filename = iconv ("UTF-8", "GBK", $ FILENAME); // Turn the characters caught by iconv from UTF-8 to GBK output
$ filename = str_replace (".", Time (). ", $ Filename); // Add time stamp after the picture name, Avoid the reimmented file coverage 文 文 文 文 文 文 文 文 文 文 文 文 文 文 文 {{{{{{{{{{{{{{{文 文 文 文 文 文 文 避. ";}} Else {echo" upload file fails ";
}
}}?>




5, some commonly used functions on PHP upload files:

file_exists() //Check whether the file or directory exists is_uploaded_file() //Judge whether the file is uploaded through HTTP POST move_uploaded_file() //Move the uploaded file to a new location is_writable() //Judge the given file name Whether it is writable iconv() //Character encoding conversion str_replace() //String replacement (change the file name to prevent duplicate names) getimagesize() //Check whether it is an image file (even if the suffix name of other types of files is changed) can be detected)


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:SAE operation recordNext article:SAE operation record