Today's written upload class, pure practiced hand, for the new students to learn

Source: Internet
Author: User
Tags getmessage

/*
*@ 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

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.