//
// + ------------------------------------------------------------------------ +
// | File Upload
| // | This code is for study and discussion only and can be modified at will.
| // | Author: whxbb (whxbb@21cn.com)
| // + ------------------------------------------------------------------------ +
//
// $ Id: upload. class. php, v 1.0 2001/10/14 14:06:57 whxbb Exp $
//
$ UPLOAD_CLASS_ERROR = array (1 => 'This format file cannot be uploaded ',
2 => 'directory not writeable ',
3 => 'File already exists ',
4 => 'Unknown error ',
5 => 'File too big'
);
/**
* Purpose
* File Upload
*
* Example
*
$ FileArr ['file'] = $ file;
$ FileArr ['name'] = $ file_name;
$ FileArr ['size'] = $ file_size;
$ FileArr ['type'] = $ file_type;
// Types of files that can be uploaded
$ Filetypes = array ('GIF', 'jpg ', 'jpg', 'PNG ');
// File Upload Directory
$ Savepath = "/usr/htdocs/upload /";
// No limit 0
$ Maxsize = 0;
// Overwrite 0. 1 is not allowed.
$ Overwrite = 0;
$ Upload = new upload ($ fileArr, $ file_name, $ savepath, $ filetypes, $ overwrite, $ maxsize );
If (! $ Upload-> run ())
{
Echo $ upload-> errmsg ();
}
*
* @ Author whxbb (whxbb@21cn.com)
* @ Version 0.1
*/
Class upload
{
Var $ file;
Var $ file_name;
Var $ file_size;
Var $ file_type;
/** Save name */
Var $ savename;
/** Save path */
Var $ savepath;
/** File format limitation */
Var $ fileformat = array ();
/** Overwrite mode */
Var $ overwrite = 0;
/** Maximum file size */
Var $ maxsize = 0;
/** File extension */
Var $ ext;
/** Error code */
Var $ errno;
/**
* Constructor
* @ Param $ fileArr file information array 'file' path and file name of the temporary file
'Name' Upload file name
'Size' size of the uploaded file
'Type' Upload file type
* @ Param savename: name of the file to be saved
* @ Param savepath: file storage path
* @ Param fileformat: Array
* @ Param overwriet indicates whether to overwrite 1. overwrite 0.
* @ Param maxsize maximum file size
*/
Function upload ($ fileArr, $ savename, $ savepath, $ fileformat, $ overwrite = 0, $ maxsize = 0 ){
$ This-> file = $ fileArr ['file'];
$ This-> file_name = $ fileArr ['name'];
$ This-> file_size = $ fileArr ['size'];
$ This-> file_type = $ fileArr ['type'];
/** Upload */
Function run ()
{
/** Check the file format */
If (! $ This-> validate_format ())
{
$ This-> errno = 1;
Return false;
}
/** Check whether the directory is writable */
If (! @ Is_writable ($ this-> savepath ))
{
$ This-> errno = 2;
Return false;
}
/** If overwriting is not allowed, check whether the file already exists */
If ($ this-> overwrite = 0 & @ file_exists ($ this-> savepath. $ this-> savename ))
{
$ This-> errno = 3;
Return false;
}
/** If the size limit exists, check whether the file exceeds the limit */
If ($ this-> maxsize! = 0)
{
If ($ this-> file_size> $ this-> maxsize)
{
$ This-> errno = 5;
Return false;
}
}
/** File upload */
If (! @ Copy ($ this-> file, $ this-> savepath. $ this-> savename ))
{
$ This-> errno = 4;
Return false;
}
/** Delete temporary files */
$ This-> destory ();
Return true;
}
/**
* File format check
* @ Access protect
*/
Function validate_format ()
{
If (! Is_array ($ this-> fileformat) // no format restrictions
Return true;
$ Ext = strtolower ($ this-> ext );
Reset ($ this-> fileformat );
While (list ($ var, $ key) = each ($ this-> fileformat ))
{
If (strtolower ($ key) ==$ ext)
Return true;
}
Reset ($ this-> fileformat );
Return false;
}
/**
* Get the file extension
* Access public
*/
Function get_ext ()
{
$ Ext = explode (".", $ this-> file_name );
$ Ext = $ ext [count ($ ext)-1];
$ This-> ext = $ ext;
}
/**
* Sets the maximum byte limit for the uploaded file.
* @ Param $ maxsize file size (bytes) 0: unlimited
* @ Access public
*/
Function set_maxsize ($ maxsize)
{
$ This-> maxsize = $ maxsize;
}
/**
* Set the overwrite mode.
* @ Param overwrite mode 1: overwrite 0: overwrite prohibited
* @ Access public
*/
Function set_overwrite ($ overwrite)
{
$ This-> overwrite = $ overwrite;
}
/**
* Set the format of files that can be uploaded
* @ Param $ array of file extensions allowed to be uploaded by fileformat
* @ Access public
*/
Function set_fileformat ($ fileformat)
{
$ This-> fileformat = $ fileformat;
}
/**
* Set the save path.
* @ Param $ savepath file storage path: ends "/"
* @ Access public
*/
Function set_savepath ($ savepath)
{
$ This-> savepath = $ savepath;
}
/**
* Set the file storage name.
* @ Savename: Save name. if it is null, the system automatically generates a random file name.
* @ Access public
*/
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 ('ymdhis ') + $ rnd;
$ Name = $ name. ".". $ this-> ext;
} Else {
$ Name = $ savename;
}
$ This-> savename = $ name;
}
/**
* Delete an object
* @ Param $ file name of the file to be deleted
* @ Access public
*/
Function del ($ file)
{
If (! @ Unlink ($ file ))
{
$ This-> errno = 3;
Return false;
}
Return true;
}
/**
* Deleting temporary files
* @ Access proctect
*/
Function destory ()
{
$ This-> del ($ this-> file );
}
/**
* Get error message
* @ Access public
* @ Return error msg string or false
*/
Function errmsg ()
{
Global $ UPLOAD_CLASS_ERROR;
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.