PHP implementation of multiple file upload method _php skills

Source: Internet
Author: User
Tags html form php define php programming

This article describes the PHP implementation of multiple file upload method. Share to everyone for your reference. The implementation method is as follows:

<?php define (' ROOT ', ' D:/program files/www/test/'); Class files_tool{protected static $allowExt =array ('. jpg ', '. jpeg ', '. png ', '. gif ', '. bmp ', '. svg ', '. chm ', '. pdf ', '. Zip '
 , '. rar ', '. Tar ', '. Gz ', '. bzip2 ', '. ppt ', '. Doc ';
 public static $wrong =array ();
 public static $path =array (); protected static $error =array (0=> ' file upload failed, no error occurred, file upload success ', 1=> ' file upload failed, upload file over php.ini
  Option limit value ', 2=> ' file upload failed, upload file size exceeds the value specified in HTML form max_file_size option ', 3=> ' file upload failed, file only partially uploaded ', 4=> ' file upload failed, no file uploaded ', 5=> ' File upload failed, disallowed suffix ', 6=> ' file upload failed, temp folder not found. PHP 4.3.10 and PHP 5.0.3 introduced ', 7=> ' file upload failed, file write failed.
  PHP 5.1.0 introduced ', 8=> ' file upload failed, did not receive the form field name ', 9=> ' file upload failed, error unknown ');
    public static function upload ($name) {//detects whether a form field is received the name if (!isset ($_files[$name))) {self:: $wrong []=8;
   return false;
   The//3 dimension array is simplified into a 2-D array $files =array_shift ($_files);
   Gets the suffix $files =self::get_ext ($files);
   Number of processing files $n =count ($files [' name ']); for ($i =0; $i < $n; $i + +) {//view current fileIf there is an error message, skip the current file and process the next file if ($files [' Error '] [$i]!=0) {self:: $wrong [$i +1]= $files [' Error '] [$i];
    Continue
     }//View the suffix of the current file, allow, if not allowed, skip the current file if (!in_array ($files [' name '] [$i],self:: $allowExt)) {self:: $wrong [$i +1]=5;
    Continue
    }//Path $dir =self::time_dir ();
    FileName $name =self::rand_name ();
    suffix $ext = $files [' name '] [$i];
    File location $path = $dir. $name. $ext;
    Moves the temporary file, if it fails, skips the current file if (!move_uploaded_file ($files [' tmp_name '] [$i], $path)) {self:: $wrong [$i]=9;
    Continue
   //Deposit Path Self:: $path [$i +1]=strtr ($path, Array (root=> '));
  Return self:: $path;
    }//Get 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;
   ///Date build path 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 filename 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]= ', $k. Self:
  : $error [$k];
  Return self:: $wrong;

 }
}

I hope this article will help you with your PHP programming.

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.