PHP upload principle and operation implementation

Source: Internet
Author: User
Tags array exit error code file upload form post html form http post ini

PHP upload file function class Library, the network has a lot of packaging is perfect, we can use directly.

This article just say 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 the image type of files, and the client maliciously to the virus file suffix name to the image file upload.

(for example, single file upload, the principle of multiple files is still unchanged, but a little bit more tips)

Index.html



    
 
  
    Upload files



    
 
  
        
  
   
        Upload file:
  
   
        
  
   
    
 
  

1, form label Enctype property

The enctype= "Multipart/form-data" in the form is used to set the MIME encoding for the form.
By default, this encoding format is application/x-www-form-urlencoded and cannot be used for file uploads;
The file data can be completely delivered only by using the Multipart/form-data and the Submit method for post.

2, MAX_FILE_SIZE hidden fields

The Max_file_size hidden Field (in bytes) must be placed before the file input field, and its value is the maximum size of the received file. This is a suggestion to the browser, and PHP will also check this.
You can simply bypass this setting on the browser side, so do not expect to block large files with this attribute. (although it's best to add this item to the form because it's friendly, it prevents users from spending time waiting to upload a large file before discovering that the file has failed to upload too much.) )

upload.php

 
  

We can see:

Array
(
    [file] => array
        (
            [name] => photo file. jpg
            [type] => image/jpeg
            [tmp_name] => F : \wamp\tmp\php41bb.tmp
            [ERROR] => 0
            [size] => 73886
        )

3, the application of global variable $_files

$_files["File" ["Name"] is the original filename of the uploaded file

$_files["File" ["type"] is the MIME type of the uploaded file

$_files["File" ["Size"] the size of the uploaded file, in bytes

$_files["File" ["Tmp_name"] file is uploaded to the server after the temporary file name ()

$_files[error code for "file" ["Error"] File upload

4, by default, the upload file will be saved in the server's temporary folder, its directory in php.ini set

PHP.ini some common settings related to file uploads:

File_uploads; Whether to allow the switch to upload files over HTTP. The default is on, which is open

Upload_tmp_dir; Files are uploaded to the server where temporary files are stored, and the system default temporary folder is used if unspecified

Upload_max_filesize; The maximum size of the file that is allowed to upload. The default is 2M

Post_max_size; Refers to the maximum value that can be received by the form Post to PHP, including all the values in the form. The default is 8M

The following is a single file upload the complete code, because it is random with writing, may be logical nesting a bit messy, understand the principle of the most important.

 -->0) {///upload file error number to determine switch ($fileError) {Case 1: $message = "uploaded file The value of the Upload_max_filesize option limit in php.ini is exceeded. 
                    ";
                Break Case 2: $message = "The size of the upload 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 = "Temp folder cannot be found. 
                    ";
                Break 
                    Case 7: $message = "file Write Failed";
                Break
                    Case 8: $message = "Due to PHP extension program interrupted file upload";
            Break

        Exit ("File upload failed:". $fileError);
            }else{if ($fileSize >100000) {//upload file for a particular form limit size exit ("Upload file exceeds limit size");
            }else{    Avoid uploading files in Chinese name garbled $fileName =iconv ("UTF-8", "GBK", $fileName);//convert Iconv to utf-8 output from GBK if (Move_uploaded_file ($tempName, "uploads/". $fileName)) {echo uploads the file successfully!
                ";
                }else{echo "Upload file failed"; }}}?>

5, about PHP upload files of some common functions: (the specific use is not posted out, see the API document bar ^_^)

File_exists check whether a file or directory exists

Is_uploaded_file to determine if the file was uploaded via an HTTP POST

Move_uploaded_file move uploaded files to a new location

Is_writable to determine if a given filename is writable

Iconv character encoding Mutual transfer

getimagesize Check for picture files (other types of files can be detected even if the suffix name is changed)



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.