会员登录 | 会员注册 | 意见建议 | 网站地图

站长资源综合门户

当前位置:首页 > 站长学院 > 编程程序 > PHP非常实用的上传类,上传效果在线演示

PHP非常实用的上传类,上传效果在线演示

时间:2012-08-11 17:34:09   作者:   来源:   点击:

名称,大小,类型,临时文件名

#*********************************************************

function

yl_getfilename($yl_type)

{

global

$_YL_UPLOAD;

return

$_FILES[$_YL_UPLOAD['yl_filedata']][$yl_type];

}

#*********************************************************

#获取文件大小

#*********************************************************

function

yl_getfilesize()

{

global

$_YL_UPLOAD;

$yl_filesize = $this ->

yl_getfilename('size');

if($yl_filesize ==

0){

$this ->

alert("请选择上传文件!");

exit;

}

if($yl_filesize

> $_YL_UPLOAD['yl_maxsize']){

switch

(strtolower($_YL_UPLOAD['yl_sizeformat'])){

case

'b':

$yl_maxsizek = $_YL_UPLOAD['yl_maxsize'] .

' B';

break;

case

'k':

$yl_maxsizek =

$_YL_UPLOAD['yl_maxsize']/1024 . '

K';

break;

case

'm':

$yl_maxsizek =

$_YL_UPLOAD['yl_maxsize']/(1024*1024) . '

M';

}

$this ->

alert("上传文件超出限制范围[".$yl_maxsizek."].K!");

exit;

}

return

$yl_filesize;

}

#*********************************************************

#获得文件扩展名

#*********************************************************

function

yl_getfiletype()

{

global

$_YL_UPLOAD;

$pathinfo = pathinfo($this -> yl_getfilename('name'));

$yl_file_ext =

strtolower($pathinfo['extension']);

//检查扩展名

if(!array_keys($_YL_UPLOAD['yl_arrext'],$yl_file_ext))

{

$this ->

alert("上传文件类型被限制!");

exit;

}

return

$yl_file_ext;

}

#*********************************************************

#上传验证

#*********************************************************

function

yl_upfile($source, $target) {

//

如果一种函数上传失败,还可以用其他函数上传

if (function_exists('move_uploaded_file')

&& @move_uploaded_file($source, $target))

{

@chmod($target, 0666);

return

$target;

} elseif (@copy($source, $target))

{

@chmod($target, 0666);

return

$target;

} elseif (@is_readable($source))

{

if ($fp = @fopen($source,'rb'))

{

@flock($fp,2);

$filedata

=

@fread($fp,@filesize($source));

@fclose($fp);

}

if

($fp = @fopen($target, 'wb')) {

@flock($fp,

2);

@fwrite($fp,

$filedata);

@fclose($fp);

@chmod

($target, 0666);

return

$target;

} else {

return

false;

}

}

}

#*********************************************************

#上传

#*********************************************************

function

yl_uploadfile()

{

global $_YL_UPLOAD;

$yl_file_path = $_YL_UPLOAD['yl_directroy'].'/'.$_YL_UPLOAD['yl_settingsnew'] ;//建立一个目录

$yl_filename = $this -> yl_getfilename('name');//原文件名

$yl_filenamenews = $_YL_UPLOAD['yl_prefix'].''.substr(time(), 5, 9).''.$_YL_UPLOAD['yl_suffix'].'';//重命名

$yl_file_size = $this -> yl_getfilesize();//获取文件大小

$yl_file_type = $this -> yl_getfiletype();//获取文件类型

if($_YL_UPLOAD['yl_ext'] ==

0){

$yl_filenamenewsext = $yl_filenamenews.'.'.$yl_file_type;//改名

}elseif ($_YL_UPLOAD['yl_ext'] == 1){

$yl_filenamenewsext = $yl_filenamenews.'.jpg';//统一改名为jpg

}

//$yl_tmp_name = str_replace(' ','',$this ->

yl_getfilename('tmp_name'));//服务器上临时文件名

$yl_tmp_name = $this -> yl_getfilename('tmp_name');//服务器上临时文件名

//检查是否已上传

if(

href="[email=!@is_uploaded_file($yl_tmp_name]!@is_uploaded_file($yl_tmp_name" _cke_saved_href="[email=!@is_uploaded_file($yl_tmp_name]!@is_uploaded_file($yl_tmp_name">mailto:!@is_uploaded_file($yl_tmp_name">!@is_uploaded_file($yl_tmp_name[/email]))

{

$this ->

alert("文件已上传!");

exit;

}

//检查目录是否存在,不存在则创建

if(

href="[email=!@is_dir(]!@is_dir(''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.''" _cke_saved_href="[email=!@is_dir(]!@is_dir(''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.''">mailto:!@is_dir(''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.''">!@is_dir(''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.''[/email]))

分享到:

网友评论

热门编程程序