<?php
/**
* File Upload class Upload
*/
Class Upload {
Const Upload_seccuss = "Upload succeeded!" Please go to the next step ... ";
Const Upload_failing = "Upload failed!";
Const Upload_errors = "Upload picture failed, is the picture file is empty, the file format is not correct, or the file exceeds the limit size";
Const NOT_UPLOAD_FILE = "Not the uploaded file, what do you want to do!!" " ;
var $id; ID Association number
var $dirStyle = 0; Set File upload directory mode 0:mdir way; 1:mdir_ym way; 2:mdir_ym_d Way
var $size; File size Unit: bytes
var $type; File type
var $path; Save back Path
var $upfile; Uploading an array of files
var $name; Name value of the "type=file" input box in form
var $isVaild =false; Verify
var $isSuccess =false; is successful
var $newpath; Paths that have been processed
var $newfilename; File names that have been processed
Const MY_DS = '/'; The path delimiter is common.
function __construct ($upfile, $path, $type = "image/pjpeg,image/jpg,image/jpeg,image/gif,image/x-png,image/png, Image/bmp ", $size =50000) {
$this->path = $path;
$this->type = Explode (', ', $type);
$this->size = $size;
$this->upfile = $upfile;
}
/**
* Associating files with ID numbers
*/
function SetId ($id) {
$this->id = $id;
}
/**
* Set File upload directory mode
*/
function Setdirstyle ($id =1) {
if ($id = = 1) {
return;
} else {
$this->dirstyle = 2;
}
}
/**
* Verify that files can be uploaded
*/
function Vaild () {
foreach ($this->type as $value) {
if ($this->upfile[' type ') = = $value && $this->upfile[' size '] > 0 &&
$this->upfile[' size '] <= $this->size) {
$this->isvaild = true;
return true;
}
}
return $this->isvaild;
}
/**
* Determine if upload is successful
*/
function issuccess () {
return $this->issuccess;
}
/**
* Date of establishment of the month
*/
function Mdir_ym ($path) {
$dir = Date ("Y-m");
$dir = Date ("Ym");
$path = str_replace (Array (' \ '), Self::my_ds, $path);
if (substr ($path, -1,1)! = Self::my_ds) {
$path. = Self::my_ds;
}
if (! file_exists ($path. $dir)) {
if (mkdir ($path. $dir)) {
$this->path= $path. $dir. Self::my_ds;
}
}else{
$this->path= $path. $dir. Self::my_ds;
}
}
/**
* Date of establishment of the month/day
*/
function Mdir_ym_d ($path) {
$dir = Date ("Y-m");
$dir = Date ("Ym");
$subdir = Date ("D");
$path = str_replace (Array (' \ '), Self::my_ds, $path);
if (substr ($path, -1,1)! = Self::my_ds) {
$path. = Self::my_ds;
}
$dirPath = $path. $dir;
$subDirPath = $dirPath. Self::my_ds. $subdir;
if (! file_exists ($dirPath)) {
if (mkdir ($dirPath)) {
if (file_exists ($dirPath) &&!file_exists ($subDirPath)) {
if (mkdir ($subDirPath)) {
$this->path= $subDirPath. Self::my_ds;
}
}
}
} else if (!file_exists ($subDirPath)) {
if (mkdir ($subDirPath)) {
$this->path= $subDirPath. Self::my_ds;
}
} else {
$this->path = $subDirPath. Self::my_ds;
}
}
/**
* Create a directory based on path
* @param $path path and path with file name
*/
static function Mdirpath ($path) {
$thePath = str_replace (Array (' \ '), Self::my_ds, $path);
$thePath = DirName (Substr_compare ($thePath, self::my_ds,0,1) = = 0? substr ($thePath, 1): $thePath);
$sub = Explode (Self::my_ds, $thePath);
$n = count ($sub);
$i = 0;
$tempPath = ";
while ($i < $n) {
$tempPath. = $sub [$i ++].self::my_ds;
if (!file_exists ($tempPath)) {
if (! mkdir ($tempPath)) {
return false;
}
}
}
if ($i >= $n) {
return true;
}
}
/**
* (Static)
* Date of establishment of the month/day
*/
static function Smdir_ym_d ($path) {
$dir = Date ("Y-m");
$dir = Date ("Ym");
$subdir = Date ("D");
$path = str_replace (Array (' \ '), Self::my_ds, $path);
if (substr ($path, -1,1)! = Self::my_ds) {
$path. = Self::my_ds;
}
$dirPath = $path. $dir;
$subDirPath = $dirPath. Self::my_ds. $subdir;
if (! file_exists ($dirPath)) {
if (mkdir ($dirPath)) {
if (file_exists ($dirPath) &&!file_exists ($subDirPath)) {
if (mkdir ($subDirPath)) {
return $subDirPath. Self::my_ds;
} else {
Return ';
}
} else {
return $subDirPath. Self::my_ds;
}
} else {
Return ';
}
}else if (!file_exists ($subDirPath)) {
if (mkdir ($subDirPath)) {
return $subDirPath. Self::my_ds;
} else {
Return ';
}
} else {
return $subDirPath. Self::my_ds;
}
}
/**
* Return file path name, not including file name
*/
function GetPath () {
return $this->path;
}
/**
* Returns the full path to the file save
*/
function GetFullPath () {
Return (Substr_compare ($this->path,self::my_ds,0,1)! = 0? self::my_ds. $this->path: $this->path). $this NewFileName;
}
/**
* Generate a new file name
*/
function Mfilename ($name) {
$names = Explode (".", $name);
$theId =!empty ($this->id)? $this->id. ' _‘ : ‘‘ ;
$this->newfilename= $theId. Uniqid (rand ()). ".". $names [Count ($names)-1];
}
/**
* Returns the new file name
*/
function Getnewfilename () {
return $this->newfilename;
}
/**
* Start uploading
*/
function Doupload () {
if ($this->vaild ()) {
if (Is_uploaded_file ($this->upfile[' Tmp_name ')) {
if ($this->dirstyle = = 1) {
$this->mdir_ym ($this->path);
} else if ($this->dirstyle = = 2) {
$this->mdir_ym_d ($this->path);
} else {
$this->path = Substr_compare ($this->path,self::my_ds,0,1)! = 0? Self::my_ds. $this->path: $this->path;
}
$this->mfilename ($this->upfile[' name ');
if (Is_uploaded_file ($this->upfile[' Tmp_name ')) {
if (Move_uploaded_file ($this->upfile[' tmp_name '), $this->path. $this->getnewfilename ())) {
$msg = Self::upload_seccuss;
$this->issuccess = true;
}else{
$msg = self::upload_failing;
}
}
} else {
$msg = Self::not_upload_file;
}
}else{
$msg = self::upload_errors. Round ($this->size/1024). " KB. ";
}
return $msg;
}
}
?>
PHP Create folder upload image