[php]
/*
Single file upload
Function
Upload files
Configure allowed suffixes
Configure allowed size
Get file suffix
Determine the suffix of the file
Error reporting
*/
class UpTool{
protected $allowExt = 'jpg,jpeg,gif,bmp,png';
protected $maxSize = 1; //1M, in M
protected $file = null; //Prepare to store uploaded file information
protected $errno = 0; //Error code
protected $error = array(
0=>'No error',
1=>'The uploaded file size exceeds the system limit',
2=>'The size of the uploaded file exceeds the web form limit',
3=>'Only part of the file was uploaded',
4=>'No files uploaded',
6=>'Temporary folder not found',
7=>'File writing failed',
8=>'Unallowed file suffix',
9=>'The file size exceeds the allowed range of the class',
10=>'Failed to create directory',
11=>'File move failed'
);
/*
Upload
*/
public function up($key) {
if (!isset($_FILES[$key])) {
return false;
} }
$f = $_FILES[$key];
//Check whether the upload is successful
if ($f['error']) {
$this->errno = $f['error'];
return false;
} }
//Get suffix
$ext = $this->getExt($f['name']);
//Check suffix
if (!$this->isAllowExt($ext)) {
$this->errno = 8;
return false;
} }
//Check size
if (!$this->isAllowSize($f['size'])) {
$this->errno = 9;
return false;
} }
//Create directory
$dir = $this->mk_dir();
if ($dir == false) {
$this->errno = 10;
return fasle;
} }
//Generate a random file name
$newname = $this->randName() . '.' .$ext;
//$dir = $dir . '/' .$newname;
//Move
if(!move_uploaded_file($f['tmp_name'], $dir . '/' .$newname)) {
$this->errno = 11;
return false;
} }
return true;//str_replace(ROOT, '', $dir);
}
public function getErr(){
return $this->error[$this->errno];
}
/*
parm string $exts allowed suffixes
Automatically add allowed suffixes and file size
*/
public function setExt($exts) {
$this->allowExt = $exts;
}
public function setSize($num) {
$this->maxSize = $num;
}
/*
string $file
return string $ext suffix
*/
protected function getExt($file) {
$tmp = explode('.', $file);
return end($tmp);
}
/*
string $ext file suffix
return bool
Prevent capitalization issues
*/
protected function isAllowExt($ext) {
return in_array(strtolower($ext), explode(',', strtolower($this->allowExt)));
}
/*
Check file size
*/
protected function isAllowSize($size) {
return $size <= $this->maxSize *1024*1024;
}
//How to create a directory by date
protected function mk_dir() {
$dir = 'images/' . date('Ym/d');
if(is_dir($dir) || mkdir($dir,0777,true)) {
Return $ DIR;
} else {
return false;
} }
}
/*
Generate a random file name
*/
protected function randName($length = 6) {
$str = 'abcdefghijkmnpqrstuvwxyz23456789';
return substr(str_shuffle($str),0,$length);
}
}
form form
[html]
Call from another page
[php]
require('./UpTool.class.php');
$uptool = new UpTool();
$uptool->setExt('rar,doc');
$uptool->setSize(1);
if ($uptool->up('pic')) {
echo 'Upload successful';
} else {
echo 'failed';
echo $uptool->getErr();
}
http://www.bkjia.com/PHPjc/477843.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477843.htmlTechArticle[php] /* Single file upload function Upload file configuration Allowed suffix configuration Allowed size Get file suffix judgment file The suffix reports an error */ class UpTool{ protected $allowExt = jp...