php文件上传到远程ftp服务器代码封装
经常在项目中遇见将文件上传到远程的ftp服务器,自己封装了一个ftp文件上传类
<?phpclassFtp{/***上传文件根目录*@varstring*/private$rootPath;/***本地上传错误信息*@varstring*/private$error='';//上传错误信息/***FTP连接*@varresource*/private$link;private$config=array('host'=>'',//服务器'port'=>21,//端口'timeout'=>90,//超时时间'username'=>'',//用户名'password'=>'',//密码);/***构造函数,用于设置上传根路径*@paramarray$configFTP配置*/publicfunction__construct($config=array()){/*默认FTP配置*/$this->config=array_merge($this->config,$config);/*登录FTP服务器*/if(!$this->login()){thrownew\Exception($this->error);}}/***检测上传根目录*@paramstring$rootpath根目录*@returnbooleantrue-检测通过,false-检测失败*/publicfunctioncheckRootPath($rootpath){/*设置根目录*/$this->rootPath=ftp_pwd($this->link).'/'.ltrim($rootpath,'/');if(!@ftp_chdir($this->link,$this->rootPath)){$this->error='上传根目录不存在!';returnfalse;}returntrue;}/***检测上传目录*@paramstring$savepath上传目录*@returnboolean检测结果,true-通过,false-失败*/publicfunctioncheckSavePath($savepath){/*检测并创建目录*/if(!$this->mkdir($savepath)){returnfalse;}else{//TODO:检测目录是否可写returntrue;}}/***保存指定文件*@paramarray$file保存的文件信息*@paramboolean$replace同名文件是否覆盖*@returnboolean保存状态,true-成功,false-失败*/publicfunctionsave($file,$replace=true){$filename='/'.$file['rootpath'].'/'.$file['savepath'].'/'.$file['savename'];//pr($filename);exit;/*不覆盖同名文件*///if(!$replace&&is_file($filename)){//$this->error='存在同名文件'.$file['savename'];//returnfalse;//}/*移动文件*/if(!ftp_put($this->link,$filename,$file['tmp_name'],FTP_BINARY)){$this->error='文件上传保存错误!';returnfalse;}returntrue;}/***创建目录*@paramstring$savepath要创建的目录*@returnboolean创建状态,true-成功,false-失败*/publicfunctionmkdir($savepath){$dir=$savepath;if(@ftp_chdir($this->link,$dir)){returntrue;}if(@ftp_mkdir($this->link,$dir)){returntrue;}elseif($this->mkdir(dirname($savepath))&&ftp_mkdir($this->link,$dir)){returntrue;}else{$this->error="目录{$savepath}创建失败!";returnfalse;}}/***获取最后一次上传错误信息*@returnstring错误信息*/publicfunctiongetError(){return$this->error;}/***登录到FTP服务器*@returnbooleantrue-登录成功,false-登录失败*/privatefunctionlogin(){extract($this->config);$this->link=ftp_connect($host,$port,$timeout);if($this->link){if(ftp_login($this->link,$username,$password)){ftp_pasv($this->link,true);returntrue;}else{$this->error="无法登录到FTP服务器:username-{$username}";}}else{$this->error="无法连接到FTP服务器:{$host}";}returnfalse;}/***析构方法,用于断开当前FTP连接*/publicfunction__destruct(){ftp_close($this->link);}}?><?php//文件调用$config=array('ftp'=>array('host'=>'192.168.111.1',//主机名称'port'=>21,//端口号'timeout'=>90,//超时时间'username'=>'ftp',账号'password'=>'123456',密码));$file=$_FILES['file'];$time=getTimeStamp();$rootdir="fireware";$dirdate=date('Ym',$time);$fileName=$file['name'];if(!in_array(substr(strrchr($fileName,'.'),1),array('rbin','ubin','bin'))){$this->ajaxReturn(405405,L('lang_dev_index12'));}$config=(array)$this->config->ftp;$ftp=newFtp($config);$rootpath=$ftp->checkRootPath($rootdir);//检测上传根目录存在不存在if(!$rootpath){$ftp->mkdir($rootdir);}$savepath=$ftp->checkSavePath("/".$rootdir.'/'.$dirdate);//检查上传目录存在不存在if(!$savepath){$ftp->mkdir($dirdate);}$data=array('rootpath'=>$rootdir,'savepath'=>$dirdate,'savename'=>$fileName);$info=array_merge($file,$data);$upinfo=$ftp->save($info);if($upinfo){//成功处理逻辑}else{//失败处理逻辑}?>
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。