This article analyzes the php file upload operation. Share to everyone for your reference, specific as follows:
File Upload
Occurs when the browser sends a request to the server.
file, for browsers, is a special type of data in the form.
Data in a browser form, two types:
String type (Byte stream encoding)
File type (binary encoding), the file is part of the form data
Server angle:
When you accept a browser request, the data in the form is processed. Use different processing methods depending on the data type:
String type, stored in $_post variable (memory)
File type data, stored in the upload temp directory
When a form is submitted, the browser defaults to the behavior:
The content in the form is a string type, and even if you add a file field, you need to increase the property on the form to tell the browser to upload more than string type data. 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>
The PHP server stores the file in a temporary directory (belonging to temporary files, valid within the script cycle) after receiving the form data of the file type.
; Temporary directory for HTTP uploaded files (would use system default if not
; specified).
http://php.net/upload- Tmp-dir
upload_tmp_dir = n.
Persistent storage of temporary files
Move_uploaded_file (Src_url,goa_url)
$_files, the information that stores the uploaded file includes the temporary address
Error type:
0-1-2-3-4-6-7
0 means no errors
1 indicates that the file is larger than the PHP setting
; Maximum allowed size for uploaded files.
; Http://php.net/upload-max-filesize
upload_max_filesize = 2M
2 indicates that the file is larger than the form setting max_file_size
<input type= ' hidden ' name= ' max_file_size ' value= ' 1024 ' >
3 indicates that the file upload is incomplete
4 means no file uploaded
5 represents a logically uploaded 0-byte file (empty text)
6 indicates no temporary upload directory found (insufficient permissions)
7 indicates a file write failure (disk space, permissions)
Maximum number of uploaded files allowed by PHP
; Maximum number of files that can is uploaded via a single request
max_file_uploads = 20
Post has a maximum value limit
Once it's over, PHP doesn't work properly. Post and file values may be null values
; Maximum size of POST data that PHP would accept.
; Http://php.net/post-max-size
post_max_size = 8M
In type detection
Suffix names and mime are all provided by browsers, requiring PHP extensions FileInfo complete checking of 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 store upload file
Principles: Business logic, number of files, time
Create directory mkdir ()
Check directory Is_dir ()
<?php upload ($_files[' file '));
function upload ($file) {if ($file [' Error ']!=0) {return false;
}//3m $max _size = 3145728;
if ($max _size< $file [' size ']) {return false; //Set the mapping relationship between a suffix name and mime $type _map = Array ('. jpeg ' =>array (' image/jpeg ', ' image/pjpeg '), '. jpg ' =>array (' imag
E/jpeg ', ' image/pjpeg '), '. png ' =>array (' image/png ', ' image/x-png '), '. gif ' =>array (' image/gif '));
Suffix $allow _ext_list = array ('. jpeg ', '. png ', '. jpg ');
$ext = Strtolower (STRRCHR ($file [' name '], '. '));
if (!in_array ($ext, $allow _ext_list)) {echo ' does not support this picture 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]);
}//Browser provides information stick $allow _mime_list = array_unique ($allow _mime_list);
if (!in_array ($file [' type '], $allow _mime_list)) {echo ' does not support this picture format ';
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 this picture format ';
return false;
}//directory store $up _loadpath = './';
$sub _dir = Date (' YMDH ');
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 ' uploaded successfully ';
return $name;
}else{echo ' upload failed ';
return false;
}
}
More about PHP Interested readers can view the site topics: "Summary of PHP file operations," "PHP Array Operation skills Encyclopedia", "PHP Basic Grammar Primer", "PHP operation and operator Usage Summary", "PHP object-oriented Programming Program", " PHP Network Programming skills Summary, "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design.