Copy CodeThe code is as follows:
Class Uploadmodel
{
protected $keys;
Protected $err = Array ();
protected $target;
protected $exts;
protected $maxSize;
protected $randName;
Protected $files = Array ();
/**
* Initialize variables
*/
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 name of file
*/
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
* Rename uploaded files @param $fileName to move individual file names
*/
Public function Save ($fileName)
{
$this->err = Array ();
$this->files = Array ();
if (!file_exists ($this->target)) {
mkdir ($this->target);
chmod ($this->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);
}
}
}
/** internal process of uploading files */
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;
}
}
/**
* is a file type that can be uploaded
* @param $file 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 file suffix name
*/
Public Function Getext ($fileName)
{
$pos = Strrpos ($fileName, '. ') +1;
Return substr ($fileName, $pos);
}
/**
* Check file size is legal
*/
Public Function checksize ($file)
{
if ($size > $this->maxsize)
{
$this->err[] = $file [' name ']. ': Max ';
return false;
}
else {
return true;
}
}
/**
* Get the uploaded file name
*/
Public Function GetFiles ()
{
return $this->files;
}
}
Usage Examples:
Copy CodeThe 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);
examples of use in Mayfish:
Copy CodeThe code is as follows:
Public function up ()
{
$U = M (' SYS ', ' upload ');
$U->target = '/tmp/';
$U->exts = ' jpg,gif ';
$U->maxsize = 1024*275; 275KB
$U->save ();
Header (' location:/?a=upload ');
}
Public function up ()
{
$U = M (' SYS ', ' upload ');
$U->target = '/tmp/';
$U->exts = ' jpg,gif ';
$U->maxsize = 1024*275; 275KB
$U->save ();
Header (' location:/?a=upload ');
}
Front Code:
Copy CodeThe code is as follows:
<title>Untitled Document</title>
http://www.bkjia.com/PHPjc/320385.html www.bkjia.com true http://www.bkjia.com/PHPjc/320385.html techarticle Copy the code as follows: PHP class Uploadmodel {protected $keys; protected $err = Array (); protected $target; protected $exts; pro Tected $maxSize; protected $randName; Protecte ...