File upload class. Example: upload. php? Phpinclude_onceupload.class.php; if ($ Submit !) {$ FileArr [file] $ file; $ fileArr [name] $ file_name; $ fileArr [size] $ file_size; $ fileArr example:
Upload. php
Include_once "upload. class. php ";
If ($ Submit! = '')
{
$ FileArr ['file'] = $ file;
$ FileArr ['name'] = $ file_name;
$ FileArr ['size'] = $ file_size;
$ FileArr ['type'] = $ file_type;
/** Supported file types */
$ Filetypes = array ('GIF', 'jpg ', 'jpg', 'PNG ');
/** File upload directory */
$ Savepath = "/usr/htdocs/upload /";
/** No maximum limit 0 unlimited */
$ 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 failed". $ upload-> errmsg ();
}
}
?>
File Upload
Upload class
Upload. class. php
//
// + ------------------------------------------------------------------------ +
// | 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'];
$ This-> get_ext ();
$ This-> set_savepath ($ savepath );
$ This-> set_fileformat ($ fileformat );
$ This-> set_overwrite ($ overwrite );
$ This-> set_savename ($ savename );
$ This-> set_maxsize ($ maxsize );
}
/** 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;
If ($ this-> errno = 0)
Return false;
Else
Return $ UPLOAD_CLASS_ERROR [$ this-> errno];
}
}
?>
Upload. php? Php include_once "upload. class. php"; if ($ Submit! = '') {$ FileArr ['file'] = $ file; $ fileArr ['name'] = $ file_name; $ fileArr ['size'] = $ file_size; $ fileArr...