<?php /** * FTP操作类 * @author chenzhouyu * * 使用$ftps = pc_base::load_sys_class('ftps');进行初始化。 * 首先通过 $ftps->connect($host,$username,$password,$post,$pasv,$ssl,$timeout);进行FTP服务器连接。 * 通过具体的函数进行FTP的操作。 * $ftps->mkdir() 创建目录,可以创建多级目录以“/abc/def/higk”的形式进行多级目录的创建。 * $ftps->put()上传文件 * $ftps->rmdir()删除目录 * $ftps->f_delete()删除文件 * $ftps->nlist()列出指定目录的文件 * $ftps->chdir()变更当前文件夹 * $ftps->get_error()获取错误信息 */ class ftps { //FTP 连接资源 private $link; //FTP连接时间 public $link_time; //错误代码 private $err_code = 0; //传送模式{文本模式:FTP_ASCII, 二进制模式:FTP_BINARY} public $mode = FTP_BINARY; /** * 连接FTP服务器 * @param string $host 服务器地址 * @param string $username 用户名 * @param string $password 密码 * @param integer $port 服务器端口,默认值为21 * @param boolean $pasv 是否开启被动模式 * @param boolean $ssl 是否使用SSL连接 * @param integer $timeout 超时时间 */ public function connect($host, $username = '', $password = '', $port = '21', $pasv = false, $ssl = false, $timeout = 30) { $start = time(); if ($ssl) { if (!$this->link = @ftp_ssl_connect($host, $port, $timeout)) { $this->err_code = 1; return false; } } else { if (!$this->link = @ftp_connect($host, $port, $timeout)) { $this->err_code = 1; return false; } } if (@ftp_login($this->link, $username, $password)) { if ($pasv) ftp_pasv($this->link, true); $this->link_time = time() - $start; return true; } else { $this->err_code = 1; return false; } register_shutdown_function(array(&$this, 'close')); } /** * 创建文件夹 * @param string $dirname 目录名, */ public function mkdir($dirname) { if (!$this->link) { $this->err_code = 2; return false; } $dirname = $this->ck_dirname($dirname); $nowdir = '/'; foreach ($dirname as $v) { if ($v && !$this->chdir($nowdir . $v)) { if ($nowdir) $this->chdir($nowdir); @ftp_mkdir($this->link, $v); } if ($v) $nowdir .= $v . '/'; } return true; } /** * 上传文件 * @param string $remote 远程存放地址 * @param string $local 本地存放地址 */ public function put($remote, $local) { if (!$this->link) { $this->err_code = 2; return false; } $dirname = pathinfo($remote, PATHINFO_DIRNAME); if (!$this->chdir($dirname)) { $this->mkdir($dirname); } if (@ftp_put($this->link, $remote, $local, $this->mode)) { return true; } else { $this->err_code = 7; return false; } } /** * 删除文件夹 * @param string $dirname 目录地址 * @param boolean $enforce 强制删除 */ public function rmdir($dirname, $enforce = false) { if (!$this->link) { $this->err_code = 2; return false; } $list = $this->nlist($dirname); if ($list && $enforce) { $this->chdir($dirname); foreach ($list as $v) { $this->f_delete($v); } } elseif ($list && !$enforce) { $this->err_code = 3; return false; } @ftp_rmdir($this->link, $dirname); return true; } /** * 删除指定文件 * @param string $filename 文件名 */ public function f_delete($filename) { if (!$this->link) { $this->err_code = 2; return false; } if (@ftp_delete($this->link, $filename)) { return true; } else { $this->err_code = 4; return false; } } /** * 返回给定目录的文件列表 * @param string $dirname 目录地址 * @return array 文件列表数据 */ public function nlist($dirname) { if (!$this->link) { $this->err_code = 2; return false; } if ($list = @ftp_nlist($this->link, $dirname)) { return $list; } else { $this->err_code = 5; return false; } } /** * 在 FTP 服务器上改变当前目录 * @param string $dirname 修改服务器上当前目录 */ public function chdir($dirname) { if (!$this->link) { $this->err_code = 2; return false; } if (@ftp_chdir($this->link, $dirname)) { return true; } else { $this->err_code = 6; return false; } } /** * 获取错误信息 */ public function get_error() { if (!$this->err_code) return false; $err_msg = array( '1' => 'Server can not connect', '2' => 'Not connect to server', '3' => 'Can not delete non-empty folder', '4' => 'Can not delete file', '5' => 'Can not get file list', '6' => 'Can not change the current directory on the server', '7' => 'Can not upload files' ); return $err_msg[$this->err_code]; } /** * 检测目录名 * @param string $url 目录 * @return 由 / 分开的返回数组 */ private function ck_dirname($url) { $url = str_replace('', '/', $url); $urls = explode('/', $url); return $urls; } /** * 关闭FTP连接 */ public function close() { return @ftp_close($this->link); } }
[分享]让你的网站和商业版一样支持附件远程FTP上传
第一步,下载附件到eextend目录下 修改附件中setconfig.php文件,把你的FTP地址和密码填写上去即可! 第二步,打开e/class/connect.php文件找到以下函数并替换。 //上传文件 function DoTranFile($file,$file_name,$file_type,$file_size,$classid,$ecms=0,$deftp=true){ global $public_r,$class_r,$doetran,$efileftp_fr; global $ftpoff,$ftphost, $ftpusername, $ftppassword, $ftpport, $ftppasv, $ftpssl, $ftptimeout,$ftpuppat,$ftpuptb; //文件类型 $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]=ECMS_PATH.$fspath['filepath'].$filepath; //附件地址 $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 { printerror('TranFail','',$ecms); } } if ($ftpoff && $deftp) { $ftp =& new ftps(); $ftp->connect($ftphost, $ftpusername, $ftppassword, $ftpport, $ftppasv, $ftpssl, $ftptimeout); // 远程存放地址 $remote = $ftpuppat . str_replace(ECMS_PATH, "", $r[yname]); if ($ftp->put($remote, $file)) { //为false时,继续上传到网站附件目录,为true时,只上传到FTP上 if(!$ftpuptb){ return $r; } } else { printerror2("FTP上传失败!",""); } } //上传文件 $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; //FileServer if($public_r['openfileserver']) { $efileftp_fr[]=$r['yname']; } return $r; } //删除附件 function DoDelFile($r){ global $class_r,$public_r,$efileftp_dr; global $ftpoff,$ftphost, $ftpusername, $ftppassword, $ftpport, $ftppasv, $ftpssl, $ftptimeout,$ftpuppat,$ftpuptb; $path=$r['path']?$r['path'].'/':$r['path']; $fspath=ReturnFileSavePath($r[classid],$r[fpath]); $delfile=ECMS_PATH.$fspath['filepath'].$path.$r['filename']; if ($ftpoff) { $ftp =& new ftps(); $ftp->connect($ftphost, $ftpusername, $ftppassword, $ftpport, $ftppasv, $ftpssl, $ftptimeout); $ftp->f_delete($ftpuppat.$fspath['filepath'].$path.$r['filename']); } DelFiletext($delfile); //FileServer if($public_r['openfileserver']) { $efileftp_dr[]=$delfile; } } //远程保存 function DoTranUrl($url,$classid,$deftp=true){ global $public_r,$class_r,$tranpicturetype,$tranflashtype,$mediaplayertype,$realplayertype,$efileftp_fr; global $ftpoff,$ftphost, $ftpusername, $ftppassword, $ftpport, $ftppasv, $ftpssl, $ftptimeout,$ftpuppat,$ftpuptb; //处理地址 $url=trim($url); $url=str_replace(" ","%20",$url); $r[tran]=1; //附件地址 $r[url]=$url; //文件类型 $r[filetype]=GetFiletype($url); if(CheckSaveTranFiletype($r[filetype])) { $r[tran]=0; return $r; } //是否已上传的文件 $havetr=CheckNotSaveUrl($url); if($havetr) { $r[tran]=0; return $r; } $string=ReadFiletext($url); if(empty($string))//读取不了 { $r[tran]=0; return $r; } //文件名 $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]=ECMS_PATH.$fspath['filepath'].$filepath; //附件地址 $r[url]=$fspath['fileurl'].$filepath.$r[filename]; //缩图文件 $r[name]=$r[savepath]."small".$r[insertfile]; //附件文件 $r[yname]=$r[savepath].$r[filename]; WriteFiletext_n($r[yname],$string); $r[filesize]=@filesize($r[yname]); //返回类型 if(strstr($tranflashtype,','.$r[filetype].',')) { $r[type]=2; } elseif(strstr($tranpicturetype,','.$r[filetype].',')) { $r[type]=1; } elseif(strstr($mediaplayertype,','.$r[filetype].',')||strstr($realplayertype,','.$r[filetype].','))//多媒体 { $r[type]=3; } else { $r[type]=0; } //FileServer if($public_r['openfileserver']) { $efileftp_fr[]=$r['yname']; } //FTP上传 if ($ftpoff && $deftp) { $ftp =& new ftps(); $ftp->connect($ftphost, $ftpusername, $ftppassword, $ftpport, $ftppasv, $ftpssl, $ftptimeout); // 远程存放地址 $remote = $ftpuppat . str_replace(ECMS_PATH, "", $r[savepath]).$r['filename']; if ($ftp->put($remote, $r["yname"])) { if(!$ftpuptb){ DelFiletext($r["yname"]); } print_r($r);exit; return $r; } else { printerror2("FTP上传失败!",""); } } return $r; } 第三步,打开e/class/functions.php,在“define('InEmpireCMSHfun',TRUE);”下面增加如下代码 //引入FTP类 require_once ECMS_PATH . 'e/extend/upftp/ftps.class.php'; //引入配置文件 require_once ECMS_PATH . 'e/extend/upftp/setconfig.php'; 第四步,同样是e/class/functions.php这个文件,找到如下函数并替换 //截取图片 function CopyImg($text,$copyimg,$copyflash,$classid,$qz,$username,$theid,$cjid,$mark){ global $empire,$public_r,$cjnewsurl,$navtheid,$dbtbpre; global $ftpoff,$ftphost, $ftpusername, $ftppassword, $ftpport, $ftppasv, $ftpssl, $ftptimeout,$ftpuppat,$ftpuptb; if(empty($text)) {return "";} if($copyimg) { $text=RepImg($text,$copyflash); } if($copyflash) {$text=RepFlash($text,$copyflash);} $exp1=""; %20 %20 %20 %20$exp2=""; $r=explode($exp1,$text); for($i=1;$i<count($r);$i++) { $r1=explode($exp2,$r[$i]); if(strstr($r1[0],"http://")||strstr($r1[0],"https://")) { $dourl=$r1[0]; } else { //是否是本地址 if(!strstr($r1[0],"/")&&$cjnewsurl) { $fileqz_r=GetPageurlQz($cjnewsurl); $fileqz=$fileqz_r['selfqz']; $dourl=$fileqz.$r1[0]; } else { $dourl=$qz.$r1[0]; } } if($mark){ $return_r=DoTranUrl($dourl,$classid,false); }else{ $return_r=DoTranUrl($dourl,$classid); } $text=str_replace($exp1.$r1[0].$exp2,$return_r[url],$text); if($return_r[tran]) { //记录数据库 $filetime=date("Y-m-d H:i:s"); //变量处理 $return_r[filesize]=(int)$return_r[filesize]; $classid=(int)$classid; $return_r[type]=(int)$return_r[type]; $theid=(int)$theid; $cjid=(int)$cjid; $sql=$empire->query("insert into {$dbtbpre}enewsfile(filename,filesize,adduser,path,filetime,classid,no,type,id,cjid,onclick,fpath) values('$return_r[filename]',$return_r[filesize],'$username','$return_r[filepath]','$filetime',$classid,'[URL]".$return_r[filename]."',$return_r[type],$theid,$cjid,0,'$public_r[fpath]');"); //加水 if($mark&&$return_r[type]==1) { GetMyMarkImg($return_r['yname']); if ($ftpoff) { $ftp =& new ftps(); $ftp->connect($ftphost, $ftpusername, $ftppassword, $ftpport, $ftppasv, $ftpssl, $ftptimeout); // 远程存放地址 $remote = $ftpuppat . str_replace(ECMS_PATH, "", $return_r[savepath]).$return_r['filename']; if ($ftp->put($remote, $return_r["yname"])) { //删除图片 if(!$ftpuptb){ DelFiletext($return_r["yname"]); } } else { printerror2("FTP上传失败!",""); } } } } } return $text; } 第五步,打开e/admin/ecmseditor/cropimg/CropImage.php 这个文件,找到“if(!file_exists($big_image_name))”把下面一行“printerror('NotCropImage','history.go(-1)');”删除或者前面加2个反斜杠。 第六步,打开e/admin/ecmseditor/cropimg/copyimgfun.php这个文件,全部替换以下代码: <?php //裁剪图片 function DoCropImage($add,$userid,$username){ global $empire,$dbtbpre,$public_r,$class_r,$tranpicturetype,$efileftp_fr,$efileftp_dr; global $ftpoff,$ftphost, $ftpusername, $ftppassword, $ftpport, $ftppasv, $ftpssl, $ftptimeout,$ftpuppat,$ftpuptb; $ftp =& new ftps(); $ftp->connect($ftphost, $ftpusername, $ftppassword, $ftpport, $ftppasv, $ftpssl, $ftptimeout); //参数处理 $pic_x=(int)$add['pic_x']; $pic_y=(int)$add['pic_y']; $pic_w=(int)$add['pic_w']; $pic_h=(int)$add['pic_h']; $doing=(int)$add['doing']; $fileid=(int)$add['fileid']; $filepass=(int)$add['filepass']; //取得文件地址 if(empty($fileid)) { printerror('NotCropImage','history.go(-1)'); } $filer=$empire->fetch1("select fileid,path,filename,classid,fpath,no from {$dbtbpre}enewsfile where fileid='$fileid'"); if(empty($filer['fileid'])) { printerror('NotCropImage','history.go(-1)'); } $path=$filer['path']?$filer['path'].'/':$filer['path']; $fspath=ReturnFileSavePath($filer['classid'],$filer['fpath']); $big_image_name=$fspath['fileurl'].$path.$filer['filename']; $up=DoTranUrlimg($big_image_name,$filer); $big_image_name=$up['yname']; if(!file_exists($big_image_name)) { printerror('NotCropImage','history.go(-1)'); } $filetype=GetFiletype($filer['filename']);//取得文件类型 if(!strstr($tranpicturetype,','.$filetype.',')) { printerror('CropImageFiletypeFail','history.go(-1)'); } //目标图片 $new_datepath=FormatFilePath($filer['classid'],'',0); $new_path=$new_datepath?$new_datepath.'/':$new_datepath; $new_insertfile=ReturnDoTranFilename($filer['filename'],0); $new_fspath=ReturnFileSavePath($filer['classid']); $new_savepath=ECMS_PATH.$new_fspath['filepath'].$new_path; $new_name=$new_savepath.$new_insertfile; //处理图片 $returnr['file']=''; $returnr['filetype']=''; if($temp_img_type = @getimagesize($big_image_name)) {preg_match('//([a-z]+)$/i', $temp_img_type[mime], $tpn); $img_type = $tpn[1];} else {preg_match('/.([a-z]+)$/i', $big_image_name, $tpn); $img_type = $tpn[1];} $all_type = array( "jpg" => array("create"=>"ImageCreateFromjpeg", "output"=>"imagejpeg" , "exn"=>".jpg"), "gif" => array("create"=>"ImageCreateFromGIF" , "output"=>"imagegif" , "exn"=>".gif"), "jpeg" => array("create"=>"ImageCreateFromjpeg", "output"=>"imagejpeg" , "exn"=>".jpg"), "png" => array("create"=>"imagecreatefrompng" , "output"=>"imagepng" , "exn"=>".png"), "wbmp" => array("create"=>"imagecreatefromwbmp", "output"=>"image2wbmp" , "exn"=>".wbmp") ); $func_create = $all_type[$img_type]['create']; if(empty($func_create) or !function_exists($func_create)) { printerror('CropImageFiletypeFail','history.go(-1)'); } //输出 $func_output = $all_type[$img_type]['output']; $func_exname = $all_type[$img_type]['exn']; if(($func_exname=='.gif'||$func_exname=='.png'||$func_exname=='.wbmp')&&!function_exists($func_output)) { $func_output='imagejpeg'; $func_exname='.jpg'; } $big_image = $func_create($big_image_name); $big_width = imagesx($big_image); $big_height = imagesy($big_image); if(!$big_width||!$big_height||$big_width<10||$big_height<10) { printerror('CropImageFilesizeFail','history.go(-1)'); } if(function_exists("imagecopyresampled")) { $temp_image=imagecreatetruecolor($pic_w,$pic_h); imagecopyresampled($temp_image, $big_image, 0, 0, $pic_x, $pic_y, $pic_w, $pic_h, $pic_w, $pic_h); } else { $temp_image=imagecreate($pic_w,$pic_h); imagecopyresized($temp_image, $big_image, 0, 0, $pic_x, $pic_y, $pic_w, $pic_h, $pic_w, $pic_h); } $func_output($temp_image, $new_name.$func_exname); ImageDestroy($big_image); ImageDestroy($temp_image); $insert_file=$new_name.$func_exname; $insert_filename=$new_insertfile.$func_exname; if(file_exists($insert_file)) { //删除原图 if(!$doing) { $empire->query("delete from {$dbtbpre}enewsfile where fileid='$fileid'"); DelFiletext($big_image_name); if($ftpoff){ $ftp->f_delete($ftpuppat.$fspath['filepath'].$path.$filer['filename']); } //FileServer if($public_r['openfileserver']) { $efileftp_dr[]=$big_image_name; } } //写入数据库 $no='[CropImg]'.$filer['no']; $filesize=filesize($insert_file); $filesize=(int)$filesize; $classid=(int)$filer['classid']; $type=1; $filetime=date("Y-m-d H:i:s"); $sql=$empire->query("insert into {$dbtbpre}enewsfile(filename,filesize,adduser,path,filetime,classid,no,type,id,cjid,fpath) values('$insert_filename','$filesize','$username','$new_datepath','$filetime','$classid','$no','$type','$filepass','$filepass','$public_r[fpath]');"); //FileServer if($public_r['openfileserver']) { $efileftp_fr[]=$insert_file; } //FTP上传 if($ftpoff){ $remote = $ftpuppat.$fspath['filepath'].$new_datepath."/".$insert_filename; $ftp->put($remote, $insert_file); if(!$ftpuptb){ DelFiletext($insert_file); } } } echo"<script>opener.ReloadChangeFilePage();window.close();</script>"; db_close(); exit(); } //保存远程图片 function DoTranUrlimg($url,$file=array()){ $classid = $file['classid']; global $public_r,$class_r,$tranpicturetype,$tranflashtype,$mediaplayertype,$realplayertype,$efileftp_fr; global $ftpoff,$ftphost, $ftpusername, $ftppassword, $ftpport, $ftppasv, $ftpssl, $ftptimeout,$ftpuppat,$ftpuptb; //处理地址 $url=trim($url); $url=str_replace(" ","%20",$url); $r[tran]=1; //附件地址 $r[url]=$url; //文件类型 $r[filetype]=GetFiletype($url); if(CheckSaveTranFiletype($r[filetype])) { $r[tran]=1; return $r; } $string=ReadFiletext($url); if(empty($string))//读取不了 { $r[tran]=3; return $r; } //文件名 $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]=ECMS_PATH.$fspath['filepath'].$filepath; //附件地址 $r[url]=$fspath['fileurl'].$filepath.$r[filename]; //缩图文件 $r[name]=$r[savepath]."small".$r[insertfile]; //附件文件 $r[yname]=$r[savepath].$r[filename]; WriteFiletext_n($r[yname],$string); $r[filesize]=@filesize($r[yname]); //返回类型 if(strstr($tranflashtype,','.$r[filetype].',')) { $r[type]=2; } elseif(strstr($tranpicturetype,','.$r[filetype].',')) { $r[type]=1; } elseif(strstr($mediaplayertype,','.$r[filetype].',')||strstr($realplayertype,','.$r[filetype].','))//多媒体 { $r[type]=3; } else { $r[type]=0; } //FileServer if($public_r['openfileserver']) { $efileftp_fr[]=$r['yname']; } return $r; } ?> 到此结束!我已经在使用了,目前没有发现其他问题,如果有发现BUG,请回复! |