PHP implementation of multiple file upload classes and usage examples _php tips

Source: Internet
Author: User
Tags file upload php class

This article describes the PHP implementation of the multiple file upload class and usage. Share to everyone for your reference, specific as follows:

1, upFiles.css.php documents

<?php class uploadfiles{Private $maxsize = ' 1000000 ';//allow maximum length of uploaded files private $allowtype = array (' jpg ', ' png ', ' gif ', ' JP //Allow upload file type private $israndfile = true;//whether random file name private $filepath;//upload path private $originName;//upload source file private $tm pfilename;//temporary file name private $newfileName//new file name private $fileSize//File size private $fileType//file type private $errorNum = 0; Error number Private $ERRORMESSG = Array ()///error message//Member initialization function __construct ($options = Array ()) {foreach ($options as $key
  => $val) {$key = Strtolower ($key);
  View whether the subscript in the incoming array is the same as the member property//print_r (Array_keys (Get_class_vars (Get_class ($this)));
  if (!in_array ($key, Array_keys (Get_class_vars (Get_class ($this)))) {continue;
  }else{$this->setoption ($key, $val);
 The Private function setOption ($key, $val) {$this-> $key = $val; echo $this->errornum. "
 <br> ";
 //Check file upload path private function Checkfilepath () {//echo $this->filepath; if (Empty ($this->filepath)) {$this->setoption (' ErrorNum ', ' 5 ');
  return false; } if (!file_exists ($this->filepath) | | |!is_writable ($this->filepath)) {if (! @mkdir ($this->filepath,0755)) {$
  This->setoption (' ErrorNum ', '-4 ');
  return false;
 } return true;
 }//Get error message Private Function GetError () {$str = "Error---Uploading file {$this->originname}";
  Switch ($this->errornum) {case 4; $str. = "No file uploaded"; Case 3;
  $str. = "file is partially uploaded"; Case 2;
  $str. = "Exceeding file form allowed size"; Case 1;
  $str. = "exceeding allowable size in php.ini"; case-1;
  $str. = "Disallowed type"; case-2; $str. = "The file is too large to exceed". $this->maxsize. "
  BYTE "; case-3;
  $str. = "Upload Failed"; case-4;
  $str. = "Create File upload directory failed"; case-5;
  $str. = "must specify an upload path"; Default
 $str. = "Unknown error"; return $STR. "
 <br> ";
 //Check file type Private function Checkfiletype () {//echo $this->filetype;
  if (!in_array (Strtolower ($this->filetype), $this->allowtype)) {$this->setoption (' ErrorNum ', '-1 ');
 return false;
 }else{return true; Check file size Private function cheCkfilesize () {if ($this->filesize > $this->maxsize) {$this->setoption (' ErrorNum ', '-2 ');
 return false;
 }else{return true;
 }///process random file name Private function Prorandfile () {$ch = $this->israndfile;
 if ($ch = = ' true ') {return true;
 }else{return false;
  }//Private function setfiles ($name = "", $tmp _name= "", $size = "", $error = "") {//Check upload path if (! $this->checkfilepath ()) {
  $this-&GT;ERRORMESSG = $this->geterror ();
 return false; }//echo $error. "
 <br> ";
  if ($error) {$this->setoption (' ErrorNum ', $error);
 return false;
 } $arrstr = Explode ('. ', $name);
 $type = End ($ARRSTR);
 $this->setoption (' Originname ', $name);
 $this->setoption (' fileSize ', $size);
 $this->setoption (' FileType ', $type);
 $this->setoption (' Tmpfilename ', $tmp _name);
 return true;
  //Check for file Upload function checkfile ($formname) {if (!@$_files[$formname]) {$this->setoption (' ErrorNum ', 4);
 return false;
 }else{return true; }///Upload file function Uploadefile ($formname) {if (! $this->checkfile ($formname)) {$this-&GT;ERRORMESSG = $this->geterror ();
 return false;
 } $return = true;
 $name = @$_files[$formname] [' name '];
 $tmp _name = @$_files[$formname] [' tmp_name '];
 $size = @$_files[$formname] [' size '];
 $error = @$_files[$formname] [' ERROR '];
 $type = $_files[$formname] [' type '];
  if (Is_array ($name)) {$errors = array (); For ($i =0 $i <count ($name); $i + +) {if ($this->setfiles ($name [$i], $tmp _name[$i], $size [$i], $error [$i])) {if (!$ This->checkfilesize () | |
   $this->checkfiletype ()) {$errors [] = $this->geterror ();
   $return = false;
   }}else{$errors [] = $this->geterror ();
  $return = false;
  if (! $return) $this->setfiles ();
  } if ($return) {$newfileN = array (); For ($i =0 $i <count ($name); $i + +) {if ($this->setfiles ($name [$i], $tmp _name[$i], $size [$i], $error [$i])) {if (!$
    This->copyfile ()) {$errors [] = $this->geterror ();
   $return = false; }else{$newfileN [] = $this->newfilename;
  }} $this->newfilename = $newfileN;
  }//print_r ($errors);
  $this-&GT;ERRORMESSG = $errors;
  Echo $errors;
 return $return;
  }else{if ($this->setfiles ($name, $tmp _name, $size, $error)) {$return = true;
  if ($error) var_dump ($error);
  if ($this->checkfilesize () && $this->checkfiletype ()) {}else{$return = false;
  }}else{$return = false;
  } if (! $return) {$this-&GT;ERRORMESSG = $this->geterror ();
 return $return;
 }//Get uploaded filename function getnewfile () {return $this->newfilename; //Copy the file to the specified path function CopyFile () {$filepath = RTrim ($this->filepath, '/'). "
 /"; if (! $this->errornum) {if ($this->prorandfile ()) {$this->newfilename = date (' Ymdhis '). Rand (1000,9999).
  $this->filetype;
  }else{$this->newfilename = $this->originname; } if (!move_uploaded_file ($this->tmpfilename, $filepath. $this->newfilename)) {$this->setoption (' ErrorNum ')
  ,-3); Return FALse
  }else{return true;
 }}else{return false;
 The message function Gteerror () {$err = $this->errormessg; After the error is uploaded
 return $err;

 }}?>

2, the use of methods

uploade.php file:

<?php
//print_r ($_files[' spic '));
Header (' Content-type:text/html;charset=utf-8 ');
if (@$_files[' spic ']) echo "ddddddddd";;
Include (' upFiles.css.php ');
$upfile = new Uploadfiles Array (' filepath ' => './upload ', ' Allowtype ' =>array (' php ', ' bmp ', ' gif ', ' jpg ', ' png '), ' Israndfile ' =>true, ' maxsize ' => ' 1000000 ');
if ($upfile->uploadefile (' spic ')) {
 $arrfile = $upfile->getnewfile ();
 foreach ($arrfile as $v) {
 echo $v, "<br/>";
 }
 echo "Upload success!" ";
} else{
 $err = $upfile->gteerror ();
 if (Is_array ($err)) {
 foreach ($err as $v 1) {
  echo $v 1, ' <br/>
 }
 }} else{
 echo $err;
 }
 Var_dump ($err);
Var_dump ($upfile);
? >

HTML file:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">  

More interested in PHP related content readers can view the site topics: "PHP file Operation Summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "Basic PHP Grammar Introduction Tutorial", "PHP operation Office Document skills Summary (including Word, Excel,access,ppt), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Tutorial", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.