主要功能:
文件上傳,獲取文件名,獲取文件大小,隨機生成新文件名,獲取文件類型,圖片生成縮略圖,返回縮略圖文件名,返回上傳后生成的文件的文件名,返回上傳后的文件路徑
//----------------------------------------------------------------------
//轉發時請保留此聲明信息,這段聲明不并會影響你的速度!
//******************* 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';
}
}
?>
使用方法:
新聞熱點
疑難解答