|
Class upfile {
// Upload file information
Var $ filename;
// Save the name
Var $ savename;
// Save path
Var $ savepath;
// The file format is limited. if it is null, the format is not limited.
Var $ format = "";
// Overwrite mode
Var $ overwrite = 0;
/* $ Overwrite = 0 does not overwrite files of the same name
* $ Overwrite = 1 overwrites the file with the same name
*/
// Maximum file bytes
Var $ maxsize = 210000000;
// File extension
Var $ ext;
/* Constructor
* $ Path: save path
* $ Format file format (separated by commas)
* $ Maxsize: maximum file size. 0 is the default value.
* $ Over override parameters
*/
Function upfile ($ path = "./", $ format = "", $ maxsize = 0, $ over = 0 ){
If (! File_exists ($ path )){
$ This-> halt ("The specified directory [". $ path. "] does not exist. ");
}
If (! Is_writable ($ path )){
$ This-> halt ("The specified directory [". $ path. "] cannot be written. ");
}
$ Path = str_replace ("", "/", $ path );
$ This-> savepath = substr ($ path,-1) = "/"? $ Path: $ path. "/"; // save path
$ This-> overwrite = $ over; // whether to overwrite the file with the same name
$ This-> maxsize =! $ Maxsize? $ This-> maxsize: $ maxsize; // maximum file size
$ This-> format = $ format;
}
/*
* Function: detects and organizes files.
* $ Form file domain name
* $ Filename: name of the file to be uploaded. The name is automatically generated by the system when it is null or multiple files are uploaded.
* $ Filename = 1. when multiple files with the same file domain name are uploaded, the file is saved as the original file name.
*/
Function upload ($ form, $ filename = ""){
If (! Isset ($ _ files [$ form]) {
$ This-> halt ("The specified file domain name does not exist. ");
} Else {
$ Filear =$ _ files [$ form];
}
If (is_array ($ filear ["name"]) {// upload multiple files with the same file domain name
$ Outfile = array (); // array of returned file names
For ($ I = 0; $ I <count ($ filear ["name"]); $ I ++ ){
$ Ar ["name"] = $ filear ["name"] [$ I];
$ Ar ["tmp_name"] = $ filear ["tmp_name"] [$ I];
$ Ar ["size"] = $ filear ["size"] [$ I];
$ Ar ["error"] = $ filear ["error"] [$ I];
$ This-> getext ($ ar ["name"]); // get the extension
$ This-> set_savename ($ filename = 1? $ Ar ["name"]: ""); // sets the file name to be saved.
$ Outfile [] = $ this-> copyfile ($ ar );
}
Return $ outfile;
} Else {// upload a single file
$ This-> getext ($ filear ["name"]); // get the extension
$ This-> set_savename ($ filename); // you can specify the file name to save.
Return $ this-> copyfile ($ filear );
}
Return false;
}
/*
* Function: detects and copies uploaded files.
* $ Filear uploads a file array
*/
Function copyfile ($ filear ){
If ($ filear ["size"]> $ this-> maxsize ){
$ This-> halt ("Upload file". $ filear ["name"]. "The size exceeds the system limit [". $ this-> maxsize. "byte] and cannot be uploaded. ");
}
If (! $ This-> overwrite & file_exists ($ this-> savename )){
$ This-> halt ($ this-> savename. "the file name already exists. ");
}
If (! $ This-> chkext ()){
$ This-> halt ($ this-> ext. "file format cannot be uploaded. ");
}
If (! Copy ($ filear ["tmp_name"], $ this-> savepath. $ this-> savename )){
$ Errors = array (0 => "file uploaded successfully ",
1 => "the uploaded file exceeds the limit of the upload_max_filesize option in php Tutorial. ini. ",
2 => "The size of the uploaded file exceeds the value specified by the max_file_size option in the html form. ",
3 => "only part of the file is uploaded. ",
4 => "No file is uploaded. ");
$ This-> halt ($ errors [$ filear ["error"]);
} Else {
@ Unlink ($ filear ["tmp_name"]); // delete a temporary file
Return $ this-> savename; // return the Upload file name
}
}
/*
* Function: get the file extension.
* $ Filename indicates the file name.
*/
Function getext ($ filename ){
If ($ filename = "") return;
$ Ext = explode (".", $ filename );
$ This-> ext = $ ext [count ($ ext)-1];
}
/*
* Function: checks whether the file type is allowed.
*/
Function chkext (){
If ($ this-> format = "" | in_array (strtolower ($ this-> ext), explode (",", strtolower ($ this-> format) return true;
Else return false;
}
/*
* Function: Set the file storage name.
* $ Savename: Save name. if it is null, the system automatically generates a random file name.
*/
Function set_savename ($ savename = ""){
If ($ savename = "") {// if no file name is set, a random file name is generated.
Srand (double) microtime () * 1000000 );
$ Rnd = revert (100,999 );
$ Name = date ('u') + $ rnd;
$ Name = $ name. ".". $ this-> ext;
} Else {
$ Name = $ savename;
}
$ This-> savename = $ name;
}
/*
* Function: Error message
* $ Msg indicates the output information.
*/
Function halt ($ msg ){
Echo"Note:". $ Msg;
Exit;
}
/*
*
* It is mainly used to delete uploaded files and does not return
* Parameter $ file: file path
*/
Function delete_file ($ file)
{
If (file_exists ($ file ))
{
$ Delete = chmod ($ file, 0777 );
$ Delete = unlink ($ file );
If (file_exists ($ file ))
{
$ Filesys = eregi_replace ("/", "", $ file );
$ Delete = system ("del $ filesys ");
Clearstatcache ();
If (file_exists ($ file ))
{
$ Delete = chmod ($ file, 0777 );
$ Delete = unlink ($ file );
$ Delete = system ("del $ filesys ");
}
}
Clearstatcache ();
}
}
}
/*
Usage
* File Upload class
Upfile ($ path = ". /", $ format =" ", $ maxsize = 0, $ over = 0): constructor parameter (save path, upload format, maximum number of uploaded bytes, whether to overwrite files of the same name)
* Instance:
// Upload the leaflet file
If (isset ($ _ files ["files"])
{
$ Filear = array ();
$ Filear = $ f-> upload ("files"); // return the uploaded file name
Echo $ filear;
}
|