Copy Code code as follows:
<?php
Class Uploadmodel
{
protected $keys;
Protected $err = Array ();
protected $target;
protected $exts;
protected $maxSize;
protected $randName;
Protected $files = Array ();
/**
* Initialize 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 name of the 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
* @param $fileName to move a single file name, rename the uploaded file
*/
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 processing upload file process
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 objects
*/
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 name of the file that has been uploaded
*/
Public Function GetFiles ()
{
return $this->files;
}
}
Use instance:
Copy Code code 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 Code code 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 ');
}
Foreground code:
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv=" Content-type " Content= "text/html; Charset=utf-8 "/>
<title> Untitled document </title>
<body>
<form action=" /?a=up "method=" post "enctype=" Multipart/form-data ">
<!--the Name property of the following two file types can be set arbitrarily, and the system will remove the input by itself- >
<input name= "files[]" type= "file" size= "/>
<input name=" files["type=" file "size="/> "
<input type= "Submit" value= "Start uploading"/>
</form>
</body>