原创文章,转载请注明: 转载自庆亮的博客-webgame架构
本文链接地址: 一个PHP上传类NdUpload
代码如下, 功能比较简单。
——————————————————————————–
系统环境:Windows XP SP2 + php 5.2.1 + mysql 5.0 + Apache 2.2.24
开发环境:Zend Studio 5.5
参数: safe_mode = Off
error_reporting = E_ALL & ~E_NOTICE
register_globals =Off
magic_quotes_runtime = Off
——————————————————————————–
nd_upload_dir = $dir_fileupload;
$this->nd_max_filesize = $max_filesize;
$this->nd_arr_ext_accepted = $arr_filetype /*array('.zip','.rar','.jpg','.gif','.bmp')*/;
$this->nd_file = $$uploadfile;
$this->nd_filename = $$uploadfilename;
$this->nd_filesize = $$uploadfilesize;
$this->nd_overWrite = $ow;
$this->nd_rename_file = $rename;
$this->nd_domain_check = 0;
$this->nd_domain = 'http://www.nd21.com/';
}
/**
* @ignore
*/
function setDir($dir)
{
$this->nd_upload_dir = $dir;
}
function getDir()
{
return $this->nd_upload_dir;
}
function copyFile()
{
//判断是否是上传文件
$file = $this->nd_file;
$filename = $this->nd_filename;
if (!is_uploaded_file($this->nd_file))
{
$this->nd_copyCode = 0;
}
else
{
//判断文件大小是否合法
if ($this->nd_filesize > $this->nd_max_filesize)
{
$this->nd_copyCode = 1;
}
else
{
//判断文件类型是否合法
if (!$this->chkExt($filename))
{
$this->nd_copyCode = 2;
}
else
{
$newfilename = $this->reName($filename);
//判断是否存在相同名称的文件
if (file_exists($newfilename))
{
//存在相同文件时是否覆盖
if ($this->nd_overWrite)
{
//判断是否删除成功
if (!unlink($newfilename))
{
$this->nd_copyCode = 4;
}
else
{
move_uploaded_file($file, $newfilename);
}
}
else
{
$this->nd_copyCode = 5;
}
}
else
{
if (move_uploaded_file($file, $newfilename))
{
$this->nd_copyCode = 6;
}
else
{
$this->nd_copyCode = 7;
}
}
}
}
}
}
function getExt($filename)
{
return strrchr($filename,'.');
}
function rndName( $name_len = 12 )
{
$allchar = "0123456789ABCDEFGHIJKLNMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_" ;
$str = "" ;
mt_srand (( double) microtime() * 1000000 );
for ( $i = 0; $i<$name_len ; $i++ )
$str .= substr( $allchar, mt_rand (0,62), 1 ) ;
return $str ;
}
/**
*@todo : 重命名文件名,并保存新文件名
*/
function reName($filename)
{
if ($this->nd_rename_file)
{
$newname = $this->rndName().$this->getExt($filename);
$this->nd_finalfile = $newname;
return $this->nd_upload_dir.$newname;
}
else
{
$this->nd_finalfile = $filename;
return $this->nd_upload_dir.$filename;
}
}
function chkExt($filename)
{
return in_array(strtolower($this->getExt($filename)), $this->nd_arr_ext_accepted);
}
function showError()
{
switch ($this->nd_copyCode)
{
case 0:$errmsg = '文件变量为非上传文件!';
break;
case 1:$errmsg = '文件体积超过系统设置!';
break;
case 2:$errmsg = '不允许的文件类型!';
break;
case 3:$errmsg = '!';
break;
case 4:$errmsg = '存在同名文件并且无法删除!';
break;
case 5:$errmsg = '存在同名文件(设置不覆盖)!';
break;
case 6:$errmsg = '文件上传成功!';
break;
case 7:$errmsg = '无法拷贝上传文件到指定位置!';
break;
case 8:$errmsg = '。!';
break;
default:
break;
}
if ($errmsg)
{
echo $errmsg."
";
}
}
}
?>
0 Comments.