linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘建议留言网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > article > Linux开发区 > WEB开发 >
栏目导栏
资料搜索
热门文章
·JSP三种页面跳转方式的比较
·LOG4J快速入门及参考资料
·Javascript设计网页中的下拉菜
·什么是PHP
·Win2003下APACHE+PHP5+MYSQL4+
·apache 的httpd.conf 文件
·MQSeries 入门手册
·php上传文件大小限制大全
·solaris 10 安装jsp大全
·半小时教你学会正则表达式
·[硬件 通讯]PHP使用RS232串口通
·如何利用JBuilder2005开发Web应
·网页技巧二十例
·JSP应用语法大全
·Linux下jsp开发环境,apache,t
最新文章
·通过Apache访问svn代码库的配置
·apache 进程数与PHP的加密
·Linux系统上WebLogic安装调试的
·在Fedora8系统下搭建JSP开发环
·Web开发设计的五大准则
·使用PHP开发SCA和SDO
·用rsync实现网站镜像和备份
·能得到你是从什么页面过来的,
·使用PHP批量生成随机用户名
·linux下编译php的配置参数总结
·在debian下为PHP5.0.3安装pdo模
·apache为用户开启主页服务
·Linux下常用php编辑工具
·java在Linux下的web应用
·面向站长和网站管理员的Web缓存
Google
 
功能控制完备的PHP上载程序
[ 作者:  加入时间:2005-11-24 21:06:41  来自: ]
上次在本站应用开发栏目发表了一篇PHP使用配置文件示例》,之后不少网友来信希望得到完整的文件上传系统的代码。实际上这个文件上传的例子在实现上仍可圈可点(谁叫我只花了一天时间来写它呢),但是功能上已经相当完备了。下面是该系统的所有代码及注释:

■主控程序 upfile.php


<?

####################################################################

#Project Name: Test

#Author : lanf 

#Home Page: http://lanf.yeah.net

#E-mail:imbusy@263.net

#Time: 2001 . 9 

####################################################################

  ##验证身份函数


function getUserRight($username,$user){

if($username==$user' '$user=="*") return true;

echo "<html>您没有这个操作ID的权限。
<a href=javascript:window.history.back()>点这里返回修改</a></html>";

return false;

}

  ##验证身份函数结束

  ##文件类型验证函数


function getFileType($20041010132811.htm,$fileType){

$fileExtent=strrchr($20041010132811.htm, ".");$fileExtent=substr($fileExtent,1); 

$fileExtent=strtolower($fileExtent);$fileType=strtolower($fileType);

if(strstr($fileType,"jpg")|
| strstr($fileType,"gif")''strstr($fileType,"png")){

if(!GetImageSize($imgfile)) {

echo "<html>这个操作ID不允许上传此种类型文件。
<a href=javascript:window.history.back()>点这里返回修改</a></html>";

return false;};

};

if(strstr($fileType,$fileExtent )' '$fileType="*") return true;

echo "<html>这个操作ID不允许上传此种类型文件。
<a href=javascript:window.history.back()>点这里返回修改</a></html>";

return false;

}

  ##文件类型验证函数结束

  ##上传站点验证函数


function checkFromSiteErr($siteFrom,$limitSite){

//取得站点域名的ip地址;http://202.108.240.75 www.yyy.com

//echo $siteFrom;

$step1=substr($siteFrom,7);//echo "<br>$step1";

$step2=strpos($step1, "/");//echo "<br>$step2";

$step3=substr($step1,0,$step2);//echo "<br>$step3";

//if(eregi("^[0-9]+.[0-9]+.[0-9]+.[0-9]+$",$step3)){;}else{

$referIP=gethostbyname($step3);//echo "<br>ip:$referIP<br>";

//}

//到这里,$referIP已经是一个上一个页面的ip地址。

//匹配相关的内容。

if(strpos($limitSite,",",1)){

//说明是一组值

$pieces = explode(",",$limitSite);

while(list($key,$value) = each($pieces)){

$limit=gethostbyname($value);

if($starPos=strpos($limit,"*")){

//带*号的IP

$tempLimit=substr($limit,0,$starPos);

$tempFromIP=substr($referIP,0,$starPos);

if($tempLimit==$tempFromIP){

return false;

}else{

continue;

};

}else{

//不带*号的IP

if($limit==$referIP){

return false;

}else{

continue;

};

};

}

echo "<html>错误的上传站点#16。
<a href=javascript:window.close()>点这里关闭窗口</a></html>";

return true;

}else{

//说明只有一个限制

$limit=gethostbyname($limitSite);

if($starPos=strpos($limit,"*")){

//带*号的IP

$tempLimit=substr($limit,0,$starPos);

$tempFromIP=substr($referIP,0,$starPos);

if($tempLimit==$tempFromIP){

return false;

}else{

echo "<html>错误的上传站点。
<a href=javascript:window.close()>点这里关闭窗口</a></html>";

return true;

};

}else{

//不带*号的IP

if($limit==$referIP){

return false;

}else{

echo "<html>错误的上传站点。
<a href=javascript:window.close()>点这里关闭窗口</a></html>";

return true;

};

};

}

};

  ##上传站点验证函数结束

  ##日志记录函数


function putLog($actionID,$message,$flag="a"){

if(!file_exists("./log")){

echo "当前目录没有建立属性为0777的log目录,正在尝试建立……";

if(@mkdir("./log",0777)){echo "建立成功!!!";}else{echo "失败!
权限不足,请手动建立 ./log 。<br> 日志没有被记录……"; return false;}

}

  $logfile="./log/".$actionID.$flag.".log";

  $cmdstr="echo "".$message."" >> ".$logfile;

  if (!file_exists($logfile)) {passthru("touch ".$logfile); 
  chmod($logfile,0777);}

  passthru($cmdstr);

  return true;

};

  ##日志记录函数

  ##取配置文件参数函数


function get_config($conf,$actionID="",$actionType=""){

//$conf 配置文件名,$actionID 操作ID ,$actionType 操作类型

global $targetDir,$user,$maxLength,$fileType,$checkFile,$limitSite,$callback;
$actionID=trim($actionID);
if($actionID=="") return false;
$keylength=strlen($actionID);
$fileArray=file($conf);
$canshuZoneId=0;
if($actionType=="upload"){

//假如是上传文件的话

while (list($key,$value) = each($fileArray)) {

$value=trim($value);

//判断参数区域,将参数提取限制在上传文件设置区

$Zone_temp=substr($value,0,1);

if($Zone_temp=="["){//开始一个参数区域

if(strstr($value, "up")){

$canshuZoneId=1;

}else{

if($canshuZoneId==0) {

continue;

}else{ 

break;

}

}

}else{

if($canshuZoneId==0) continue;

}//区域选择结束

$id_temp=substr($value,0,$keylength);

if($id_temp==$actionID){

//开始取参数

$callback=trim(strrchr($value, " ")); 

$value=trim(substr($value,0,strlen($value)-strlen($callback)));

$limitSite=trim(strrchr($value, " "));

$value=trim(substr($value,0,strlen($value)-strlen($limitSite)));

$checkFile=trim(strrchr($value, " ")); 

$value=trim(substr($value,0,strlen($value)-strlen($checkFile)));

$fileType=trim(strrchr($value, " ")); 

$value=trim(substr($value,0,strlen($value)-strlen($fileType)));

$maxLength=trim(strrchr($value, " ")); 

$value=trim(substr($value,0,strlen($value)-strlen($maxLength)));

$user=trim(strrchr($value, " ")); 

$value=trim(substr($value,0,strlen($value)-strlen($user)));

$targetDir=trim(strrchr($value, " "));

$value=trim(substr($value,0,strlen($value)-strlen($targetDir)));

return true;

}else{

continue;

}

}//while结束,能运行到这一步说明没有找到匹配的ID

return false;

}elseif($actionType=="del"){

//如果是删除文件操作

while (list($key,$value) = each($fileArray)) {

$value=trim($value);

//判断参数区域,将参数提取限制在 删除 文件设置区

$Zone_temp=substr($value,0,1);

if($Zone_temp=="["){//开始一个参数区域

if(strstr($value, "del")){

$canshuZoneId=1;

}else{

if($canshuZoneId==0) {

continue;

}else{ 

break;

}

}

}else{

if($canshuZoneId==0) continue;

}//区域选择结束

$id_temp=substr($value,0,$keylength);

if($id_temp==$actionID){

//开始取参数

$user=trim(strrchr($value, " ")); 

return true;

}else{

continue;

}



}//while结束,能运行到这一步说明没有找到匹配的ID



return false;

}else{//既不是up,也不是del

return false;

};



return false;

}

  ##取配置文件参数函数结束


//主程序

$configureFile="config.inc.php";

$actiontype=trim($actiontype);



##上传文件

if($actiontype=="upload"){

if(!($actionid && $username && $password && $userfile_name)) {

echo "<html>操作ID、用户名、密码、上传文件缺一不可!
<a href=javascript:window.history.back()>点这里返回修改</a></html>";

exit;

}



if(!get_config($configureFile,$actionid,$actiontype))
{echo "<html>不合法的操作ID。<a href=javascript:window.history.back()>
点这里返回修改</a></html>";exit;}



if($userfile_size>$maxLength*1024){echo "<html>上传的文件超过规定大小的上限。
<a href=javascript:window.history.back()>点这里返回修改</a></html>";exit;}



if(!getUserRight($username,$user) 
&& !getFileType($userfile_name,$fileType)){exit;} //可调整

if($limitSite!="*") if(checkFromSiteErr($HTTP_REFERER,$limitSite)) exit;



if($checkFile!="*") {

$cmd_return_var=passthru($checkFile." ".$userfile_name);
/命令行参数要求问检查文件名 + 空格 + 上传文件名

//$cmd_return_var=true;//程序开发的时候使用,请删除
if(!$cmd_return_var){

echo "<html>您上传的文件没有通过我们的检查,请上传符合要求的文件。
<a href=javascript:window.history.back()>点这里返回修改</a></html>";

exit;

}

};//调用外部程序检查,不符合要求的文件返回 false,符合要求返回true;



if(""==trim($newname)){$newname=$userfile_name;};//处理文件名

$targetFileName=$targetDir."/".$newname;



if(file_exists($targetFileName)){

$oldTargetSize=filesize($targetFileName);

if($userfile_size<=$oldTargetSize ){ 

echo "<html>您要上传的目标文件已经存在。上传动作被取消。
<a href=javascript:window.history.back()>点这里返回修改</a></html>";

exit;

}

};



copy($userfile,$targetFileName) or die;

chmod($targetFileName,0666);//110110110

//日志信息

$message=date("Y-m-d H:i:s")." remote_ip:".$REMOTE_ADDR." 
UserName:".$username." FileName:".$newname."";

putLog($actionid,$message);



header("location:".

Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
分页:[1] 2
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
无相关信息