/*
*@ File Upload
*@ to obtain an upload instance, use Upload::getinstance (); Please refer to the class interface for the specific use method
* @auther Sauteed
* @email [email protected]
* @blogwww. 92zyw.com
* */
Class upload{
private static $instance = null;
protected $destDir;//storage Directory of uploaded files
protected $allowType;//type of upload allowed
protected $denyType;//reject the type of file to upload
protected $mutilUpload;//Whether multiple file uploads are allowed
protected $maxUpload;//maximum number of uploaded bytes
Private function __construct (array $options =null) {
$this->destdir = ' upload ';/* str_replace (' \ \ ', '/', Strstr (GETCWD (), ' test ', true)); */
$this->allowtype = array (' jpg ', ' gif ');
$this->denytype = Array (' PHP ');
$this->mutilupload=false;
$this->maxupload = ' 202800 ';
Is_dir ($this->destdir) or mkdir ($this->destdir);
if (Isset ($options)) {
foreach ($options as $k = = $v) {
$this $k = $v;
}
}
}
Single-Case mode
public static function getinstance (array $options =null) {
if (static:: $instance = = null) {
if (Isset ($options)) {
Static:: $instance = new Upload ($options);
}else{
Static:: $instance = new Upload ();
}
}
return static:: $instance;
}
/* *
* Upload a single file
* @param array
*
* */
Public function upload (array $file) {
try {
return $this->move ($file [' name '], $file [' tmp_name ']);
} catch (Uploadexception $e) {
Exceptionpage::showmsg ($e. GetMessage (), $e->geterrno);
}
}
/*
* Multiple File Upload
* @ received parameter is array format such as: Array (' name ' = = $file [' name], ' tmp_name ' = = $file [' Tmp_file '])
*
* */
Public function Mutilupload (array $files) {
if (! $this->uploadavailable ()) {
throw new Uploadexception ("Do not allow multiple file uploads");
return false;
}
foreach ($files as $file) {
if (!isset ($file) &&!is_array ($file)) {
throw new Uploadexception (' Please verify that the array parameters meet the requirements of multiple file uploads! ');
}
$this->move ($file [' name '], $file [' tmp_name ']);
}
return true;
}
/* Copy File
* @param string
* @param string
* */
protected function Move ($file, $tmp _name) {
$filename = $this->destdir. $file;
$fileTmpName = Date (' Ymdhis ', Time ()). '. '. PathInfo ($file) [' extension '];
$result = Move_uploaded_file ($tmp _name, "{$this->destdir}/{$fileTmpName}");
if (!result) {
throw new Uploadexception (' file does not exist or cannot be moved because of some unpredictable cause);
}
return $result;
}
protected function uploadavailable ($file) {
$size = FileSize ($file);
if ($size > $this->maxupload) {
return false;
}
return false;
}
}
/* Exception class */
Class expection{
protected $msg;
protected $errno;
Public Function GetMessage () {
return $this->msg;
}
Public Function Geterrno () {
return $this->errno;
}
}
/* Upload Exception class */
Class Uploadexpection extends expection{
Public function __construct ($msg, $errno =null) {
$this->msg = $msg;
if (Isset ($errno)) {
$this->errno =null;
}else{
$this->errno = $errno;
}
}
}
Class exceptionpage{
static $instance = null;
Public $html = ';
Public function __construct ($msg, $errno = ") {
$html. = ' $html. = ' $html. = ' <title> exception page </title> ';
$html. = ' $html. = ' <body> ';
$html. = "<div><p> exception: {$msg}</p>";
if (Isset ($errno)) {
$html. = ' <p> exception code: {$errno}</p> ';
}
$html. = ' </div> ';
$html. = ' </body> ';
$html. = ' Echo $html;
}
public static function ShowMsg ($msg, $errno) {
New Exception ($msg, $errno);
}
}
Today's written upload class, pure practiced hand, for the new students to learn