Usage and precautions of $ _ FILES in PHP

Source: Internet
Author: User
Tags error code file size file upload html form php tutorial

The $ _ FILES array contains the following content:

$ _ FILES ['myfile'] ['name'] original name of the client file

$ _ FILES ['myfile'] ['type'] MIME type of the file, which must be supported by the browser, for example, "image/gif"

$ _ FILES ['myfile'] ['size'] size of the uploaded file, in bytes

$ _ FILES ['myfile'] ['tmp _ name'] temporary file name stored on the server after the file is uploaded. It is generally the default file name in php. the upload_tmp_dir of ini is specified, but the putenv () function does not work.

$ _ FILES ['myfile'] ['error'] error code related to the file upload. ['error'] was added in PHP 4.2.0, the following is its description: (they become constants after PHP3.0)

UPLOAD_ERR_ OK value: 0; no error occurred. The file is uploaded successfully.


UPLOAD_ERR_INI_SIZE: 1; the uploaded file exceeds the limit of the upload_max_filesize option in php. ini.

UPLOAD_ERR_FORM_SIZE value: 2; the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.

UPLOAD_ERR_PARTIAL value: 3; only part of the file is uploaded.

UPLOAD_ERR_NO_FILE value: 4; no file is uploaded; value: 5; size of the uploaded file is 0

Note:

1. After the file is uploaded, it is stored in the temporary directory by default. In this case, it must be deleted from the temporary directory or moved to another place. If it does not exist, it will be deleted. That is, no matter whether the upload is successful or not, the files in the temporary directory will be deleted after the script is executed. Therefore, you must copy the file to another location using the copy () function of PHP before deleting the file. In this case, the file upload process is completed.

2. Before PHP 4.1.0, the array name is $ HTTP_POST_FILES, which is not an automatic global variable like $ _ FILES. PHP 3 does not support the $ HTTP_POST_FILES array.


3. When using form to upload FILES, you must add the attribute content enctype = "multipart/form-data". Otherwise, an exception is reported when you use $ _ FILES [filename] to obtain file information.


Test

The code is as follows: Copy code

<Html>
<Body>

<Form action = "upload_file.php tutorial" method = "post"
Enctype = "multipart/form-data">
<Label for = "file"> filename: </label>
<Input type = "file" name = "file" id = "file"/>
<Br/>
<Input type = "submit" name = "submit" value = "submit"/>
</Form>

</Body>
</Html>

Php code

The code is as follows: Copy code

<? Php

If ($ _ files ["file"] ["type"] = "image/gif ")
| ($ _ Files ["file"] ["type"] = "image/jpeg ")
| ($ _ Files ["file"] ["type"] = "image/pjpeg "))
& ($ _ Files ["file"] ["size"] <20000 ))
  {
If ($ _ files ["file"] ["error"]> 0)
    {
Echo "error:". $ _ files ["file"] ["error"]. "<br/> ";
    }
Else
    {
Echo "upload:". $ _ files ["file"] ["name"]. "<br/> ";
Echo "type:". $ _ files ["file"] ["type"]. "<br/> ";
Echo "size:". ($ _ files ["file"] ["size"]/1024). "kb <br/> ";
Echo "stored in:". $ _ files ["file"] ["tmp_name"];
    }
  }
Else
  {
Echo "invalid file ";
  }

?>

Simplified file upload code

The code is as follows: Copy code

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = utf-8"/>
<Title> Excel data acquisition demonstration </title>
<Meta name = "Keywords" content = "TODO"/>
<Meta name = "Description" content = "TODO"/>
</Head>
<Body>
<Div>
<Div> submit a form </div>
<Div>
<Form method = "POST" action = "www.111cn.net/Index/parse" enctype = "multipart/form-data">
<Input type = "file" name = "excel" value = ""/>
<Input type = "submit" name = "submit" value = "submit"/>
</Form>
</Div>
</Div>
</Body>
</Html>

Public function parse ()
    {
/**
* $ _ FILES array description
* Array (n ){
* ["Form file box name"] => array (5 ){
* ["Name"] => name of the submitted file
* ["Type"] => The submitted file type is "application/vnd. ms-Excel"
* ["Tmp_name"] => temporary file name
* ["Error"] => error (0 Success 1. The file size exceeds the value of upload_max_filesize2. The file size exceeds that of MAX_FILE3. The file size is incomplete. 4. The file is not uploaded)
* ["Size"] => file size (unit: KB)
*}
*}
*/
$ Return = array (0 ,'');
/**
* Determine whether to submit
* Is_uploaded_file (file name) is used to determine whether the specified file is uploaded using the POST method to prevent unauthorized submission. It is usually used together with move_upload_file to save the uploaded file to the specified path.
*/
If (! Isset ($ _ FILES) |! Is_uploaded_file ($ _ FILES ['Excel '] ['tmp _ name'])
        {
$ Return = array (1, 'illegal submission ');
        }
// Process
If (0 = $ return [0])
        {
Import ('@. Util. ExcelParser ');
$ Excel = new ExcelParser ($ _ FILES ['Excel '] ['tmp _ name']);
$ Return = $ excel-> main ();
        }
// Output processing
Print_r ($ return );
?>

For more details, see: http://www.111cn.net/phper/21/4662008ae09ff36c1a029763e9dcacf2.htm

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.