CopyCode The Code is as follows: <? PHP
Class uploadmodel
{
Protected $ keys;
Protected $ err = array ();
Protected $ target;
Protected $ exts;
Protected $ maxsize;
Protected $ randname;
Protected $ files = array ();
/**
* Initialize the variable
*/
Public Function _ construct ()
{
$ This-> exts = array ('jpeg ', 'jpg', 'gif', 'png ', 'zip', 'rar ');
$ This-> maxsize = 1024*1024*2;
$ This-> Target = dirname (_ file _). '/upload /';
$ This-> randname = true;
$ This-> keys = $ this-> getkeys ();
}
/**
* Get the file name
*/
Protected function getkeys ()
{
$ Keys = array_keys ($ _ files );
Return $ keys;
}
/**
* Set different types of Variables
*/
Public Function _ set ($ name, $ value)
{
$ Property = array ('target', 'exts', 'maxsize', 'randname ');
If (! In_array ($ name, $ property) return false;
Switch (strval ($ name ))
{
Case 'target ':
$ This-> $ name = configure: Read ('app _ path'). $ value;
Break;
Case 'exts ':
$ This-> $ name = explode (',', $ value );
Break;
Case 'randname ':
If ($ value = true | $ value = 1)
{
$ This-> $ name = true;
}
Else {
$ This-> $ name = false;
}
Break;
Default:
$ This-> $ name = $ value;
}
}
/**
* Move uploaded files to the specified directory
* @ Param $ filename when moving the name of a single file, rename the uploaded file.
*/
Public Function save ($ filename)
{
$ This-> err = array ();
$ This-> files = array ();
If (! File_exists ($ this-> Target )){
Mkdir ($ this-> target );
Chmod ($ this-& gt; target, 0777 );
}
Foreach ($ this-> keys as $ key)
{
If (is_array ($ _ FILES [$ key] ['name'])
{
$ COUNT = count ($ _ FILES [$ key] ['name']);
For ($ I = 0; $ I <$ count; $ I ++)
{
$ Keys = array_keys ($ _ FILES [$ key]);
Foreach ($ keys as $ item)
{
$ File [$ item] =$ _ FILES [$ key] [$ item] [$ I];
}
$ This-> upload ($ file, $ filename );
}
Return (count ($ this-> ERR)> 0 )? False: true;
}
Else {
Return $ this-> upload ($ _ file [$ key], $ filename );
}
}
}
/** Process of uploading files internally */
Protected function upload ($ file, $ filename)
{
If (! Is_uploaded_file ($ file ['tmp _ name']) return false;
If (! $ This-> checkext ($ file) return false;
If (! $ This-> checksize ($ file) return false;
If ($ this-> randname)
{
$ Newfilename = $ this-> target. date ('ymdhis ', time ()). rand (0, 9 ). '. '. $ this-> getext ($ file ['name']);
}
Elseif (emptyempty ($ filename ))
{
$ Newfilename = $ this-> target. '/'. $ file ['name'];
}
Else {
$ Newfilename = $ this-> target. '/'. $ filename;
}
$ Result = move_uploaded_file ($ file ['tmp _ name'], $ newfilename );
If (! $ Result)
{
Return false;
}
Else {
$ This-> files [] = str_replace ($ this-> Target, $ newfilename );
Return true;
}
}
/**
* Indicates whether the file type can be uploaded.
* @ Param $ file object
*/
Public Function checkext ($ file)
{
If (! In_array ($ this-> getext ($ file ['name']), $ this-> exts ))
{
$ This-> err [] = $ file ['name']. ': ext ';
Return false;
}
Else {
Return true;
}
}
/**
* Get the file suffix
*/
Public Function getext ($ filename)
{
$ Pos = strrpos ($ filename, '.') + 1;
Return substr ($ filename, $ POS );
}
/**
* Check whether the file size is valid.
*/
Public Function checksize ($ file)
{
If ($ size> $ this-> maxsize)
{
$ This-> err [] = $ file ['name']. ': Max ';
Return false;
}
Else {
Return true;
}
}
/**
* Get the name of the uploaded file
*/
Public Function getfiles ()
{
Return $ this-> files;
}
}
instance used:
copy Code the code is as follows: Include 'uploaded. model. PHP ';
$ u = new uploadmodel ();
$ U-> Target ='/tmp /';
$ U-> exts = 'jpg, GIF ';
$ U-> maxsize = 1024*275; // 275kb
$ U-> Save ();
$ files = $ U-> getfiles ();
print_r ($ files );
include 'uploaded. model. PHP ';
$ u = new uploadmodel ();
$ U-> Target ='/tmp /';
$ U-> exts = 'jpg, GIF ';
$ U-> maxsize = 1024*275; // 275kb
$ U-> Save ();
$ files = $ U-> getfiles ();
print_r ($ files );
example of use in mayfish: copy Code the code is as follows: public function up ()
{< br> $ u = m ('sys ', 'upload');
$ U-> Target = '/tmp/';
$ U-> exts = 'jpg, gif ';
$ U-> maxsize = 1024*275; // 275kb
$ U-> Save ();
header ('location :/? A = upload');
}< br> Public Function up ()
{< BR >$ u = m ('sys ', 'upload ');
$ U-> Target = '/tmp/';
$ U-> exts = 'jpg, GIF ';
$ U-> maxsize = 1024*275; // 275kb
$ U-> Save ();
header ('location :/? A = upload');
}
front-end code: copy Code the code is as follows:
untitled document