PHP mobile Internet Development notes (6)-file upload/download

Source: Internet
Author: User

I. File Upload

1. Client settings:

(1) Specify the values of enctype and method in the <form> label.

Enctype = "multipart/form-data"; Method = "POST"

(2) Set a hidden type input box in the form. The value of name is the hidden value of MAX_FILE_SIZE.

2. Server Side settings:

(1) $ _ FILES multi-dimensional array: used to store information related to various uploaded FILES

(2) file upload and php configuration file settings, such as the following commands in the php. ini file

Command Default Value Function Description

File_uploadsON: Determine whether the PHP script on the server can upload HTTP files

Memory_limit8M sets the maximum memory size that can be allocated by the script to prevent out-of-control scripts from occupying the server memory exclusively.

Upload_max_file2M limit the maximum size of uploaded files processed by PHP. The value must be smaller than the value of POST_MAX_SIZE.

Post_max_size8M limits the maximum value of information that can be accepted through the POST Method

Upload_tmp_dirNULL: Temporary path of the file to be uploaded. It can be an absolute path.


 

3. PHP file upload and resource commands

File_uploads (boolean)

Whether to enable the http post file upload function

Max_execution_time (integer)

Maximum PHP script execution time

Memory_limit (integer) Unit: M

Maximum memory for running PHP scripts

Upload_max_filesize (integer) Unit: M

PHP maximum size of uploaded files

Upload_tmp_dir (string)

Temporary location of uploaded file storage

Post_max_size (integer) Unit: M

Maximum size of http post data

4. $ _ FILES Array

$ _ FILES ['userfile'] [size]

Obtains the number of bytes of the uploaded file.

$ _ FILES ['userfile'] ['type']

Obtain the MIME type of the uploaded file. Each MIME type is composed of the primary and child types separated "/".

$ _ FILES ['userfile'] ['error']

Get the error code of the uploaded file. Value 0: no error. The file is successfully uploaded. Value 1: the size of the uploaded file exceeds the value specified by the upload_max_filesize option in the PHP configuration file. Value 2: the size of the uploaded file exceeds the value specified by MAX_FILE_SIZE in the HTML form. 3: indicates that the file is only partially uploaded; 4: indicates that no file is uploaded.

$ _ FILES ['userfile'] ['name']

Get the original name of the uploaded file, including the extension

$ _ FILES ['userfile'] ['tmp _ name']

Obtain the temporary location name of the uploaded file, which is the file name specified in the temporary directory.

5. File upload function

Is_upload_file

Checks whether the specified file is uploaded through http post.

Bool is_upload_file (string $ filename)

Move_upload_file

Move the uploaded file to a new location

Bool move_upload_file (string $ filename, string $ destination)

Note: After a file is uploaded, it is first stored in the temporary directory of the server. You can use this function to move the uploaded file to a new location. Compared with copy () and move, it can detect and ensure that the file specified by the first filename parameter is a legally uploaded file.

6. Error Message description

UPLOAD_ERR_ OK {value = 0}

UPLOAD_ERR_INI_SIZE {value = 1}

UPLOAD_ERR_FORM_SIZE {value = 2}

UPLOAD_ERR_PARTIAL {value = 3}

UPLOAD_ERR_NO_FILE {value = 4}

UPLOAD_ERR_NO_TMP_DIR {value = 6}

UPLOAD_ERR_CANT_WRITE {value = 7}

<?phpprint_r($_FILES);?>

Ii. File Download

To download a simple file, you only need to mark it with the HTML link <a> and specify the downloaded file with the url value of the href attribute. This method can only process MIME-type files that cannot be recognized by some browsers.

<Html> 
 

To improve security, if you do not want to provide a file link in tag a, you must send necessary header information to the browser. We can use the following code.

<? Php // send the header information of the specified file MIME type header ("content-type: application/x-msdownload"); $ fileName = $ _ GET ['filenmae']; $ path = "[$ fileName]"; // sends the header information of the description file, and the attachment and file name header (content-disposition: attachment: filename = [$ fileName]); readfile ($ path);?>

Iii. File function library

Touch

Set file access and modification time

Bool touch (string $ filename [, int $ time [, int $ time])

Copy

Copy a file

Bool copy (string $ source, string $ dest)

Note: Use the rename function to move files.

File_put_contents

Write a string to a file

Int file_put_contents (string $ filename, string $ data [, int $ flag [, resource $ content])

File_get_contents

Read the entire file to a string

String file_get_contents (string $ filename [, bool user_include_path [, resource $ content [, int $ offset [, int $ maxlen])


Iv. serialization and deserialization

Serialize

Serialization

String serialize (mixed $ value)

Note: serialize () can process any type except resouce. Even serialize () contains arrays that point to its own reference.

Unserialize

Deserialization

Mixed unserialize (string $ str)

<? Php $ var = "abcdefj"; // serialization $ arr = serialize ($ var); echo 'serialization <br> '; var_dump ($ arr ); echo '<br>'; // deserialization echo 'deserialization <br>'; $ arr1 = unserialize ($ arr); var_dump ($ arr1 ); echo '<br>';?>

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.