您当前的位置:首页 > CMS常见问题 > 帝国cms常见问题 >

帝国cms上传文件的功能代码注释

0
<?php
//上传文件
//file 文件在Web服务器中临时存储的位置
//file_name 用户系统中的文件名称
//file_type 文件的MIME类型
//file_size 文件的字节大小
function DoTranFile($file,$file_name,$file_type,$file_size,$classid,$ecms=0){
	global $public_r,$class_r,$doetran,$efileftp_fr;
	$classid=(int)$classid;
	$r[filetype]=GetFiletype($file_name);//文件类型
	$r[insertfile]=ReturnDoTranFilename($file_name,$classid);//文件名
	$r[filename]=$r[insertfile].$r[filetype];//在服务器上最终保存的文件名
	$r[filepath]=FormatFilePath($classid,$mynewspath,0);//日期目录
	$filepath=$r[filepath]?$r[filepath]."/":$r[filepath];
	$fspath=ReturnFileSavePath($classid);
	$r[savepath]=eReturnEcmsMainPortPath().$fspath['filepath'].$filepath;//D:\phpStudy\WWW\pinku\d/file/p/2017-07-31/
	$r[url]=$fspath['fileurl'].$filepath.$r[filename];//附件地址
	$r[name]=$r[savepath]."small".$r[insertfile];//缩图文件
	$r[yname]=$r[savepath].$r[filename];//附件文件
	$r[tran]=1;
	//保留扩展名验证
	if(CheckSaveTranFiletype($r[filetype])){
		if($doetran){
			$r[tran]=0;
			return $r;
		}else{
			//请查看目录权限是否为0777, 文件上传不成功
			printerror('TranFail','',$ecms);
		}
	}
//bool move_uploaded_file(string $filename, string $destination)
//本函数检查并确保由filename指定的文件是合法的上传文件(即通过PHP的HTTP POST上传机制所上传的), 如果文件合法, 则将其移动为由destination指定的文件。
//成功时返回TRUE
//如果filename不是合法的上传文件, 不会出现任何操作, move_uploaded_file()将返回FALSE
//如果filename是合法的上传文件, 但出于某些原因无法移动, 不会出现任何操作, move_uploaded_file()将返回FALSE。此外还会发出一条警告。
	$cp=@move_uploaded_file($file,$r[yname]);
	if(empty($cp)){
		if($doetran){
			$r[tran]=0;
			return $r;
		}else{
			printerror('TranFail','',$ecms);
		}
	}
	DoChmodFile($r[yname]);
	$r[filesize]=(int)$file_size;
	if($public_r['openfileserver']){
		$efileftp_fr[]=$r['yname'];
	}
	return $r;
}
//取得文件扩展名
function GetFiletype($filename){
	$filer=explode(".",$filename);
	$count=count($filer)-1;
	return strtolower(".".RepGetFiletype($filer[$count]));
}
function RepGetFiletype($filetype){
	$filetype=str_replace('|','_',$filetype);
	$filetype=str_replace(',','_',$filetype);
	$filetype=str_replace('.','_',$filetype);
	return $filetype;
}
//返回上传文件名
function ReturnDoTranFilename($file_name,$classid){
	$filename=md5(uniqid(microtime()));
	return $filename;
}
//格式化附件目录
function FormatFilePath($classid,$mynewspath,$enews=0){
	global $public_r;
	if($enews){
		$newspath=$mynewspath;
	}else{
		//文件夹命名规则
		//Y-m-d、Y/m-d、Y/m/d、Ymd、空字符串
		$newspath=date($public_r['filepath']);
	}
	if(empty($newspath)){
		return "";
	}
	$fspath=ReturnFileSavePath($classid);
	$path=eReturnEcmsMainPortPath().$fspath['filepath'];//D:\phpStudy\WWW\pinku\d/file/p/
	$returnpath="";
	$r=explode("/",$newspath);
	$count=count($r);
	for($i=0;$i<$count;$i++){
		if($i>0){
			$returnpath.="/".$r[$i];
		}else{
			$returnpath.=$r[$i];
		}
		$createpath=$path.$returnpath;//D:\phpStudy\WWW\pinku\d/file/p/2017-07-31
		$mk=DoMkdir($createpath);//依次创建新目录
		if(empty($mk)){
			//建立目录不成功, 请检查目录权限。
			printerror("CreatePathFail","");
		}
	}
	return $returnpath;//返回2017-07-31字符串
}
//返回附件目录
function ReturnFileSavePath($classid,$fpath=""){
	global $public_r,$class_r;
	//附件存放目录
	//0(栏目目录)
	//1(/d/file/p目录)
	//2(/d/file目录)
	$fpath=$fpath||strstr(",".$fpath.",",",0,")?$fpath:$public_r["fpath"];
	$efileurl=eReturnFileUrl();
	if($fpath==1)//p目录
	{
		$r['filepath']='d/file/p/';
		$r['fileurl']=$efileurl.'p/';
	}
	elseif($fpath==2)//file目录
	{
		$r['filepath']='d/file/';
		$r['fileurl']=$efileurl;
	}
	else//栏目目录
	{
		if(empty($classid))
		{
			$r['filepath']='d/file/p/';
			$r['fileurl']=$efileurl.'p/';
		}
		else
		{
			$r['filepath']='d/file/'.$class_r[$classid][classpath].'/';
			$r['fileurl']=$efileurl.$class_r[$classid][classpath].'/';
		}
	}
	return $r;
}
//返回附件域名地址
function eReturnFileUrl($ecms=0){
	global $public_r;
	if($ecms==1){
		//fileurl附件地址/d/file/
		return $public_r['fileurl'];
	}
	$fileurl=$public_r['openfileserver']?$public_r['fs_purl']:$public_r['fileurl'];
	return $fileurl;
}
//返回主端根目录
function eReturnEcmsMainPortPath(){
	global $ecms_config;
	if($ecms_config['sets']['mainportpath']){
		return $ecms_config['sets']['mainportpath'];
	}else{
		return ECMS_PATH;
	}
}
//建立目录函数
function DoMkdir($path){
	global $public_r;
	//不存在则建立
	if(!file_exists($path)){
		//安全模式
		if($public_r['phpmode']){
			$pr[0]=$path;
			FtpMkdir($ftpid,$pr,0777);
			$mk=1;
		}else{
//bool mkdir(string $pathname [,int $mode=0777 [,bool $recursive=false [,resource $context]]])
//尝试新建一个由pathname指定的目录
//pathname 目录的路径
//mode 默认的mode是0777, 意味着最大可能的访问权(mode在Windows下被忽略), 注意也许想用八进制数指定模式, 也就是说该数应以零打头, 模式也会被当前的umask修改, 可以用umask()来改变。
//recursive(递归) 为TRUE时表示允许创建多级目录
//context 在PHP 5.0.0中增加了对上下文的支持
//返回值 成功时返回TRUE, 或者在失败时返回FALSE。
			$mk=@mkdir($path,0777);

//bool chmod(string $filename, int $mode)
//尝试将filename所指定文件的模式改成mode所给定的
//filename 文件的路径
//mode 注意mode不会被自动当成八进制数值, 而且也不能用字符串(例如"g+w")。要确保正确操作, 需要给mode前面加上0
//参数包含三个八进制数按顺序分别指定了所有者、所有者所在的组以及所有人的访问限制。每一部分都可以通过加入所需的权限来计算出所要的权限。
//数字1表示使文件可执行(执行权限)
//数字2表示使文件可写(写权限)
//数字4表示使文件可读(读权限)
//加入这些数字来制定所需要的权限。

//所有者可读写, 其他人没有任何权限
//chmod("/somedir/somefile", 0600);

//所有者可读写, 其他人可读
//chmod("/somedir/somefile", 0644);

//所有者有所有权限, 其他所有人可读和执行
//chmod("/somedir/somefile", 0755);

//所有者有所有权限, 所有者所在的组可读和执行
//chmod("/somedir/somefile", 0750);

//Note: 当前用户指的是执行PHP的用户。很可能和通常的shell或者FTP用户不是同一个。在大多数系统下文件模式只能被文件所有者的用户改变。
//Note: 此函数不能作用于远程文件, 被检查的文件必须是可通过服务器的文件系统访问的。
//成功时返回TRUE, 或者在失败时返回FALSE
			@chmod($path,0777);
		}
		if(empty($mk)){
			echo str_replace(ECMS_PATH,'/',$path);
			printerror("CreatePathFail","history.go(-1)");
		}
	}
	return true;
}
//保留扩展名验证
function CheckSaveTranFiletype($filetype){
	$savetranfiletype=',.php,.php3,.php4,.php5,.php6,.asp,.aspx,.jsp,.cgi,.phtml,.asa,.asax,.fcgi,.pl,.ascx,.ashx,.cer,.cdx,.pht,.shtml,.shtm,.stm,';
	if(stristr($savetranfiletype,','.$filetype.',')){
		return true;
	}
	return false;
}
//设置上传文件权限
function DoChmodFile($file){
	global $public_r;
	//文件生成权限, 0为0777, 1为不限制
	if($public_r['filechmod']!=1){
		@chmod($file,0777);
	}
}
?>

建站咨询

在线咨询真诚为您提供专业解答服务

咨询热线

137 1731 25507×24小时服务热线

微信交流

二维码终于等到你,还好我没放弃
返回顶部