PHP Mobile Internet Development Notes (5)--Upload and download of files

Source: Internet
Author: User
Tags end error code file size file upload getdate string string format strlen

First, the file upload

1, client settings:

(1), in

Label Enctype and method two properties to indicate the corresponding values.

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

(2), form form to set up a hidden type of input box, where the value of name is max_file_size hidden value

2, server-side settings:

(1), $_files Multidimensional array: Used to store all kinds of uploaded files related to Information

(2), File Upload and PHP configuration file settings, such as the following php.ini files in some instructions

Instruction default Function description

File_uploads on determines whether the PHP script on the server can accept HTTP file uploads

Memory_limit 8M Sets the maximum amount of memory the script can allocate to prevent runaway script exclusive server Ram

Upload_max_file 2M limits the maximum size of PHP processing uploaded files, this value must be less than Post_max_size value

Post_max_size 8M limits the maximum value of information that can be accepted by post method

Upload_tmp_dir NULL upload file temporary path, can be an absolute path

3, php file upload and resource instructions

File_uploads (Boolean)

Whether to turn on the HTTP Post file upload function

Max_execution_time (integer)

Maximum execution time for PHP scripts

Memory_limit (integer) unit m

Maximum memory for PHP scripts to run

Upload_max_filesize (integer) unit m

Maximum size of PHP upload file

Upload_tmp_dir (String)

To upload a temporary location for a file store

Post_max_size (integer) unit m

Maximum size of HTTP post data

4, $_files array

$_files[' UserFile '][size]

Get the number of bytes to upload files

$_files[' userfile ' [' type ']

Gets the MIME type of the uploaded file, each of which is composed of the main type and subtype delimited by "/"

$_files[' userfile ' [' Error ']

Get the error code for the uploaded file, 0: No error, file upload success; 1: Upload file size exceeds the value of the upload_max_filesize option in the PHP configuration file; 2: The upload file size exceeds the value specified in the HTML form max_file_size 3: Indicates that the file is only partially uploaded; 4: Indicates that no files have been uploaded.

$_files[' UserFile ' [' Name ']

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

$_files[' UserFile ' [' Tmp_name ']

Gets the temporary location name of the uploaded file, which is the file name that is stored in the temporary directory.

5. File Upload function

Is_upload_file

Determines whether the specified file is uploaded via an HTTP Post

BOOL Is_upload_file (String $filename)

Move_upload_file

Move uploaded files to a new location

BOOL Move_upload_file (String $filename, String $destination)

Note: After the file is uploaded, it is first stored in the temporary directory of the server, which can be used to move the uploaded file to a new location, and it can detect and ensure that the file specified by the first parameter filename is a legally uploaded file, compared to copy () and moving ().

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}




Second, File download

Simple file downloads only need to use HTML link tags and specify the URL value of the attribute href to the downloaded file. This method can only handle MIME type files that are not recognized by browsers.




In order to improve security, do not want to give a file link in the A tag, you must send the necessary header information to the browser, we can use the following code.

third, file function library

Touch

To set the access and grooming time for a file

BOOL Touch (string $filename [, int $time [, int $time]])

Copy

Copying files

BOOL Copy (string $source, String $dest)

Note: Please use the rename function to move the file

File_put_contents

Writes 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 of

String serialize (mixed $value)

Note: Serialize () can handle any type other than Resouce. You can even serialize () those that contain arrays that point to their own references.

Unserialize

Deserialization

Mixed Unserialize (String $str)



Date

Format a local time/date

String Date (string format[, int timestamp]);

GetDate

Get Date/Time information

Array getdate ([int timestamp]);


Microtime

Returns the current UNIX timestamp and number of microseconds

Mixed Microtime ([bool get_as_float]);

Date_default_timezone_set

Set the default time zone

BOOL Date_default_timezone_set (string timezone_identifier);

Date_default_timezone_get

Get the default time zone

String date_default_timezone_get (void);

Three, character function library

Strlen

Get string length

int strlen (string $str);

Strtolower

Convert string to lowercase

String Strtolower (string $str);

Strtoupper

Convert string to uppercase

String Strtoupper (string $str);

Ucfirst

Capitalize first letter

String Ucfirst (string $str);

Ucwords

Converts the first letter of each word to uppercase letters

String Ucwords (string $str);

LTrim

Remove spaces or other characters from the beginning of a string

String LTrim (String $str [, String $charlist]);

RTrim

Remove spaces or other characters from the end of a string

String RTrim (String $str [, String $chirlist]);

Trim

Remove spaces or other characters from the start and end of a string

Str_replace

Character substitution

Mixed Str_ireplace (mixed $search, mixed $replace, mixed $subject, [int $count]);

Str_ireplace

Character substitution

Mixed Str_ireplace (mixed $search, mixed $replace, mixed $subject, [int $count]);

MD5

Computes the MD5 hash of a string

String MD5 (string $str [, bool $raw _output=false]);

Strpos

Returns the position of a character at the first occurrence of another character

int Strpos (String haystack, mixed needle[, int offset]);






Related Article

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.