|
上次在本站应用开发栏目发表了一篇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论坛讨论
|
|
|
|
|
|