PHP upload principle and operation implementation _php tutorial

Source: Internet
Author: User

PHP upload principle and operation implementation


About PHP upload file function class Library, there are many packages on the Internet is perfect, we can directly use it. This article is just about the upload principle and simple upload operation, the veteran will ignore ha ^_^ ~ There are some security judgments, such as: The server limit can receive image types of files, and the client malicious virus file suffix name changed to image matching file upload. (for example, single file upload, multi-file principle is still the same, but a little bit more tips) index.html copy code <title>Upload files</title> Copy code 1, form label Enctype property sheet enctype= "Multipart/form-data" is the MIME encoding used to set up the form. By default, this encoding format is application/x-www-form-urlencoded and cannot be used for file uploads; only Multipart/form-data is used and the submission method is post for full file data transfer. 2. max_file_size hidden field max_file_size the hidden field (in bytes) must precede the file input field, and its value is the maximum size of the received file. This is a suggestion for the browser, PHP will also check this. This setting can be bypassed easily on the browser side, so do not expect this feature to block large files. (However, it is best to add this item to the form because it avoids the hassle of having to wait for a large file to be uploaded before the user discovers that the file is too large to upload.) ) upload.php We can see: Copy code Array ([file] = + Array ([name] + = photo file. jpg [Type] = Image/jpeg [Tmp_name] = F:\wamp\tmp\php41BB . tmp [ERROR] = 0 [size] + 73886)) Copy Code 3, global variable $_files application $_files[' file ' [' name '] for the original file name of the uploaded file $_files[' file '] [ ' type '] for the MIME type of the uploaded file $_files[' file ' [' size '] uploaded files, in bytes $_files[' file ' [' Tmp_name '] files are uploaded on the server after the temporary file name () $_  files[' file ' [' Error '] upload error code 4, by default, the upload file will be saved in the server side of the temporary folder, its directory in php.ini set php.ini and file upload some of the common settings: file_uploads; Whether to allow the switch to upload files over HTTP.  The default is on, which is open upload_tmp_dir;  Files uploaded to the server where temporary files are stored, if not specified, the system will be the default temporary folder Upload_max_filesize; That is, the maximum size allowed for uploading files.  The default is 2M post_max_size; Refers to the maximum value that can be received by a form post to PHP, including all values in the form. Default is 8M the following is a single file upload complete code, because it is random with write, may be a bit of logic nesting, understand the principle of the most important. Copy Code 0) {//Upload file error number to determine switch ($fileError) {Case 1: $message = "The uploaded file exceeds the value of the Upload_max_filesize option limit 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 = "The file is only partially uploaded. "; Break Case 4: $message = "No files were uploaded. "; Break Case 6: $message = "The temporary folder cannot be found. "; Break Case 7: $message = "file Write Failed"; Break Case 8: $message = "File upload interrupted due to PHP extension"; Break } exit ("File upload failed:". $fileError); }else{if ($fileSize >100000) {//Limit the size of the upload file for a particular form exit ("Upload file exceeds the limit size");} else{//Avoid uploading the file's Chinese name garbled $fileName =iconv ("UTF-8", "GBK", $fileName);//The character encoding Iconv crawled from utf-8 to GBK output if (move_uploaded_file ($tempName, "uploads/". $fileName)) {echo ' uploads the file successfully! "; } else{echo "Upload file Failed";}} }}?> copy code 5, about the PHP upload files of some common functions: (the specific usage will not be posted, look at the API Document bar ^_^) file_exists Check whether the file or directory exists is_uploaded_file determine if the file is through HTTP POS T upload move_uploaded_file to move the uploaded file to a new location is_writable determine if the given file name is writable iconv character encoding getimagesize Check whether it is a picture file (other types of files even if the suffix is changed to is detected)

http://www.bkjia.com/PHPjc/854420.html www.bkjia.com true http://www.bkjia.com/PHPjc/854420.html techarticle PHP upload principle and operation to implement the PHP upload file function class Library, there are many packages on the Internet is perfect, we can directly use it. This article is just about the upload principle and Jane ...

  • 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.