PHP creates a folder to upload images

Source: Internet
Author: User

<? PHP
/**
* File Upload class upload
*/
Class upload {

Const upload_seccuss = "Upload successful! Go to the next step ...";
Const upload_failing = "Upload Failed! ";
Const upload_errors = "failed to upload the image. It indicates that the image file is empty, the file format is incorrect, or the file size exceeds the limit ";
Const not_upload_file = "not an uploaded file. What do you want !! ";
VaR $ ID; // ID associated number
VaR $ dirstyle = 0; // set the File Upload directory mode to 0: mdir; 1: mdir_ym; 2: mdir_ym_d
VaR $ size; // file size unit: bytes
VaR $ type; // file type
VaR $ path; // Save the returned path
VaR $ upfile; // upload a file Array
// Var $ name; // name value of the "type = file" input box in Form
VaR $ isvaild = false; // verify
VaR $ issuccess = false; // whether the request is successful
// Var $ newpath; // path that has been processed
VaR $ newfilename; // processed file name

Const my_ds = '/'; // common path Separators

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;
}

/**
* Associate a file with an ID.
*/
Function setid ($ id ){
$ This-> id = $ ID;
}

/**
* Set the File Upload directory method.
*/
Function setdirstyle ($ id = 1 ){
If ($ id = 1 ){
Return;
} Else {
$ This-> dirstyle = 2;
}
}

/**
* Verify whether the file 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 whether the upload is successful
*/
Function issuccess (){
Return $ this-> issuccess;
}

/**
* Create a Daily recording by 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;
}
}

/**
* Create a Daily recording by month/month
*/
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 the path
* @ Param $ 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)
* Create a Daily recording by month/month
*/
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;
}
}

/**
* The returned file path name, excluding the file name
*/
Function getpath (){
Return $ this-> path;
}

/**
* Returns the complete path for saving the file.
*/
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];
}

/**
* Return the new file name.
*/
Function getnewfilename (){
Return $ this-> newfilename;
}

/**
* Start upload
*/
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-& gt; size/1024). "kb .";
}
Return $ MSG;
}


}
?>

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.