PHP file upload operation example details,
This document analyzes the PHP file upload operations. 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"
<body> <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="file"> </br> <input type="submit" value="submit"> </form></body>
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.
<input type='hidden' name='MAX_FILE_SIZE' value='1024'>
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 ()
<? Phpupload ($ _ FILES ['file']); function upload ($ file) {if ($ file ['error']! = 0) {return false;} // 3 M $ max_size = 3145728; if ($ max_size <$ file ['SIZE']) {return false ;} // set the mime ing between a suffix name and mime $ type_map = array ('.jpeg '=> 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 ;}}