主要功能:
文件上传,获取文件名,获取文件大小,随机生成新文件名,获取文件类型,图片生成缩略图,返回缩略图文件名,返回上传后生成的文件的文件名,返回上传后的文件路径
//----------------------------------------------------------------------
//转发时请保留此声明信息,这段声明不并会影响你的速度!
//******************* ieb上传类 v1.1 *********************************
//作者:卢韦华
//网站:http://www.iebsoft.cn
//电子邮件:[email protected]
//版权声明:版权所有,源代码公开,各种用途均可免费使用,但是修改后必须把修改后的文件
//发送一份给作者.
//***********************************************************************
//
class ieb_upload{
var $formname; //文件域名称
var $directroy; //上传至目录
var $maxsize; //最大上传大小
var $canupload; //是否可以上传
var $doupfile; //上传的文件名
var $sm_file; //缩略图名称
var $error; //错误参数
function ieb_upload($formname='', $dirpath='', $maxsize=2097152) //(1024*2)*1024=2097152 就是 2m
{
global $formname, $directroy, $maxsize, $canupload, $error, $doupfile, $sm_file;
//初始化各种参数
$formname = $formname;
$maxsize = $maxsize;
$canupload = true;
$doupfile = '';
$sm_file = '';
$error = 0;
if ($formname == ''){
$canupload = false;
$error = 1;
break;
}
if ($dirpath == ''){
$directroy = $dirpath;
}else{
$directroy = $dirpath.'/';
}
}
//检查文件是否存在
function scanfile()
{
global $formname, $error, $canupload;
if ($canupload){
$scan = is_readable($_files[$formname]['name']);
if ($scan){
$error = 2;
}
return $scan;
}
}
//获取文件大小
function getsize($format = 'b')
{
global $formname, $error, $canupload;
if ($canupload){
if ($_files[$formname]['size'] == 0){
$error = 3;
$canupload = false;
}
switch ($format){
case 'b':
return $_files[$formname]['size'];
break;
case 'm':
return ($_files[$formname]['size'])/(1024*1024);
}
}
}
//获取文件类型
function getext()
{
global $formname, $error, $canupload;
if ($canupload){
$ext=$_files[$formname]['name'];
$extstr=explode('.',$ext);
$count=count($extstr)-1;
}
return $extstr[$count];
}
//获取文件名称
function getname()
{
global $formname, $canupload;
if ($canupload){
return $_files[$formname]['name'];
}
}
//新建文件名
function newname()
{
global $canupload, $formname;
if ($canupload){
$fullname=$_files[$formname]['name'];
$extstr=explode('.',$fullname);
$count=count($extstr)-1;
$ext = $extstr[$count];
return date('ymdhis').rand(0,9).'.'.$ext;
}
}
//上传文件
function upload($filename = '')
{
global $formname, $directroy, $canupload, $error, $doupfile;
if ($canupload){
if ($_files[$formname]['size'] == 0){
$error = 3;
$canupload = false;
return $error;
break;
}
}
if($canupload){
if ($filename == ''){
$filename = $_files[$formname]['name'];
}
$[email protected]($_files[$formname]['tmp_name'], $directroy.$filename);
if($doupload)
{
$doupfile = $filename;
chmod($directroy.$filename, 0777);
return true;
}else{
$error = 4;
return $error;
}
}
}
//创建图片缩略图
function thumb($dscchar='',$width=150,$height=113)
{
global $canupload, $error, $directroy, $doupfile, $sm_file;
if ($canupload && $doupfile != ''){
$srcfile = $doupfile;
if ($dscchar == ''){
$dscchar = 'sm_';
}
$dscfile = $directroy.$dscchar.$srcfile;
$data = getimagesize($directroy.$srcfile,&$info);
switch ($data[2]) {
case 1:
$im = @imagecreatefromgif($directroy.$srcfile);
break;
case 2:
$im = @imagecreatefromjpeg($directroy.$srcfile);
break;
case 3:
$im = @imagecreatefrompng($directroy.$srcfile);
break;
}
$srcw=imagesx($im);
$srch=imagesy($im);
$ni=imagecreatetruecolor($width,$height);
imagecopyresized($ni,$im,0,0,0,0,$width,$height,$srcw,$srch);
$cr = imagejpeg($ni,$dscfile);
chmod($dscfile, 0777);
if ($cr){
$sm_file = $dscfile;
return true;
}else{
$error = 5;
return $error;
}
}
}
//显示错误参数
function err(){
global $error;
return $error;
}
//上传后的文件名
function upfile(){
global $doupfile, $error;
if ($doupfile != ''){
return $doupfile;
}else{
$error = 6;
}
}
//上传文件的路径
function filepath(){
global $directroy, $doupfile, $error;
if ($doupfile != ''){
return $directroy.$doupfile;
}else{
$error = 6;
}
}
//缩略图文件名称
function thumbmap(){
global $sm_file, $error;
if ($sm_file != ''){
return $sm_file;
}else{
$error = 6;
}
}
//显示版本信息
function ieb_version(){
return 'ieb_upload class ver 1.1';
}
}
?>
使用方法:
新闻热点
疑难解答