Php file upload operation examples

Source: Internet
Author: User
Tags php file upload
This article mainly introduces php file upload operations, and analyzes the implementation skills and precautions of PHP file transfer operations in combination with the instance form. For more information, see the example in this article. We will share this with you for your reference. The details are as follows:

File Upload

Occurs in the request sent by the browser to the server.

File, for the browser, is a special type of data in the form.

There are two types of data in the browser form:

String type (byte stream encoding)

File type (binary encoding). a file is a part of the form data.

Server:

When accepting browser requests, process the data in the form. Use different processing methods based on different data types:

String type, stored in $ _ POST variable (memory)

FILE-type data, stored in the temporary Upload Directory

When the form is submitted, the browser will default behavior:

The content in the form is of the string type. even if a file field is added, you need to add attributes to the form to inform the browser that more than the string type data is uploaded. Enctype = "multipart/form-data"

  

After receiving form data of the file type, the php server stores the file in the temporary directory (which is a temporary file and valid within the script cycle)

; Temporary directory for HTTP uploaded files (will use system default if not; specified).; http://php.net/upload-tmp-dir;upload_tmp_dir =

Store temporary files persistently

move_uploaded_file(src_url,goa_url)

$ _ FILES: stores the information of the uploaded file, including the temporary address.

Error Type:

0-1-2-3-4-6-7

0 indicates no error

1 indicates that the file is larger than the php setting.

; Maximum allowed size for uploaded files.; http://php.net/upload-max-filesizeupload_max_filesize = 2M

2 indicates that the file is larger than the max_file_size set in the form.

 

3 indicates incomplete file Upload

4 indicates that no file is uploaded.

5 indicates that a 0-byte file (Empty file) is uploaded logically)

6 indicates that the temporary Upload directory is not found (the permission is insufficient)

7 indicates file write failure (disk space and permission)

Maximum number of uploaded files allowed by php

; Maximum number of files that can be uploaded via a single requestmax_file_uploads = 20

Max post size limit

If the value of post and file is exceeded, php cannot process the post and file values normally. it may be null.

; Maximum size of POST data that PHP will accept.; http://php.net/post-max-sizepost_max_size = 8M

Detecting type

The extension fileinfo of php is required to check the file information (function process and object-oriented)

; Extension = php_fileinfo.dll

$finfo = new Finfo(FILEINFO_MIME_TYPE);$mine_type = $finfo->file($file['tmp_name']);

Molecular Directory storage Upload files

Principles: business logic, number of files, and time

Create directory mkdir ()

Check the is_dir () Directory ()

 Array ('image/jpeg ', 'image/pjpeg'), '.jpg '=> array ('image/jpeg', 'image/pjpeg '), '.png '=> array ('image/png', 'image/x-png '), '.gif' => array ('image/GIF ')); // suffix $ allow_ext_list = array('.w.','.png', '.jpg '); $ ext = strtolower (strrchr ($ file ['name'],'. '); if (! In_array ($ ext, $ allow_ext_list) {echo 'does not support the image format'; return false;} // MIME $ allow_mime_list = array (); foreach ($ allow_ext_list as $ val) {$ allow_mime_list = array_merge ($ allow_mime_list, $ type_map [$ val]);} // The browser provides information persistence $ allow_mime_list = array_unique ($ allow_mime_list); if (! In_array ($ file ['type'], $ allow_mime_list) {echo 'This image format is not supported '; return false ;} // php self-check $ file_mime = new Finfo (FILEINFO_MIME_TYPE); $ mime = $ file_mime-> file ($ file ['tmp _ name']); if (! In_array ($ mime, $ allow_mime_list) {echo 'does not support the image format'; return false;} // Directory Storage $ up_loadpath = '. /'; $ sub_dir = date ('ymmdh'); if (! Is_dir ($ up_loadpath. $ sub_dir) {mkdir ($ up_loadpath. $ sub_dir) ;}$ prefix = 'bee _ '; $ name = uniqid ($ prefix, true ). $ ext; if (move_uploaded_file ($ file ['tmp _ name'], $ up_loadpath. $ sub_dir. $ name) {echo 'upload succeeded '; return $ name;} else {echo 'upload failed'; return false ;}}

I hope this article will help you with PHP programming.

For more details about PHP file upload operations, refer to PHP Chinese network!

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.