Multifile upload-PHP
- Define ('root', 'd:/Program Files/www/test /');
- Class Files_Tool {
- Protected static comment ');
- Public static $ wrong = array ();
- Public static $ path = array ();
- Protected static $ error = array (
- 0 => 'File Upload failed, no error occurred, file upload succeeded ',
- 1 => 'File Upload failed. the uploaded file exceeds the limit of the upload_max_filesize option in php. ini ',
- 2 => 'File Upload failed. The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form ',
- 3 => 'File Upload failed, only part of the file is upload ',
- 4 => 'File Upload failed, no file was upload ',
- 5 => 'File Upload failed, suffix not allowed ',
- 6 => 'File Upload failed. the temporary folders. PHP 4.3.10 and PHP 5.0.3 cannot be found ',
- 7 => 'File Upload failed, file writing failed. PHP 5.1.0 introduced ',
- 8 => 'File Upload failed. the NAME of the form field is not received ',
- 9 => 'File Upload failed, error unknown'
- );
-
- Public static function upload ($ name ){
- // Check whether the NAME of the form field is received
- If (! Isset ($ _ FILES [$ name]) {
- Self: $ wrong [] = 8;
- Return false;
- }
- // The 3-dimensional array is simplified to a 2-dimensional array.
- $ Files = array_shift ($ _ FILES );
- // Obtain the suffix
- $ Files = self: get_Ext ($ files );
- // Number of file processing times
- $ N = count ($ files ['name']);
- For ($ I = 0; $ I <$ n; $ I ++ ){
- // Check whether the current file has an error message. If yes, skip the current file and process the next file.
- If ($ files ['error'] [$ I]! = 0 ){
- Self: $ wrong [$ I + 1] = $ files ['error'] [$ I];
- Continue;
- }
- // Check whether the suffix of the current file is allowed. if not, skip the current file.
- If (! In_array ($ files ['name'] [$ I], self: $ allowExt )){
- Self: $ wrong [$ I + 1] = 5;
- Continue;
- }
- // Path
- $ Dir = self: time_Dir ();
- // File name
- $ Name = self: rand_Name ();
- // Suffix
- $ Ext = $ files ['name'] [$ I];
- // File location
- $ Path = $ dir. $ name. $ ext;
- // Move the temporary file. if it fails, the current file will be skipped.
- If (! Move_uploaded_file ($ files ['tmp _ name'] [$ I], $ path )){
- Self: $ wrong [$ I] = 9;
- Continue;
- }
- // Storage path
- Self: $ path [$ I + 1] = strtr ($ path, array (ROOT => ''));
-
- }
- Return self: $ path;
- }
-
- // Obtain the suffix
- Protected static function get_Ext ($ arr ){
- If (! Is_array ($ arr) |! Isset ($ arr ['name']) {return false ;}
- Foreach ($ arr ['name'] as $ k => $ v ){
- $ Arr ['name'] [$ k] = strtolower (strrchr ($ v ,'.'));
- }
- Return $ arr;
- }
- // Generate path by date
- Protected static function time_Dir (){
- $ Dir = ROOT. 'data/images/'. date ('Y/m/d/', time ());
- If (! Is_dir ($ dir )){
- Mkdir ($ dir, 0777, true );
- }
- Return $ dir;
- }
- // Generate random file names
- Protected static function rand_Name (){
- $ Str = str_shuffle ('1234567890qwertyuiopasdfghjklzxcvbnm ');
- $ Str = substr ($ str, 0, 6 );
- Return $ str;
- }
- // Error interface
- Public static function errors (){
- Foreach (self ::$ wrong as $ k => $ v ){
- Self: $ wrong [$ k] = 'quarter '. $ k.'. self: $ error [$ k];
- }
- Return self: $ wrong;
- }
-
- }
|