原文博客地址http://xgs888.top/post/view?id=97;

PHP针对ftp文件的操作方法,如果是只操作一个ftp,可以使用里面的单利模式,

不需要每次都去实例化,我的项目中需要去链接很多个ftp服务器;

所以需要多次去连接和关闭;

<?php/***CreatedbyPhpStorm.*ftp的文件操作类*User:xiaoxie*Date:2018/5/7*Time:17:44**/namespaceApp\Tools;classFtpFile{staticprivate$_instance=null;private$ftp=null;public$off;//返回操作状态(成功/失败)//私有的构造方法publicfunction__construct($config){//实例化$this->ftp=@ftp_connect($config['ftp_ip'],$config['ftp_port'])ordie("FTPconnectionfail");//登录验证@ftp_login($this->ftp,$config['ftp_username'],$config['ftp_password']);//是否开启被动模式if(isset($config['ftp_pasv'])){@ftp_pasv($this->ftp,true);}}/***CreatedbyPhpStorm.*function:getInstance*Description:公有的静态方法*User:Xiaoxie*Email736214763@qq.com*@returnFtpFile|null**/staticpublicfunctiongetInstance($config){if(!(self::$_instanceinstanceofself)){self::$_instance=newFtpFile($config);}returnself::$_instance;}/***CreatedbyPhpStorm.*function:up_file*Description:上传文件*User:Xiaoxie*Email736214763@qq.com*@param$path本地路径*@param$newpath若目标目录不存在则新建*@parambool$type**/functionup_file($path,$newpath,$type=true){if($type)$this->dir_mkdirs($newpath);$this->off=@ftp_put($this->ftp,$newpath,$path,FTP_BINARY);if(!$this->off){return"文件上传失败,请检查权限及路径是否正确!";}else{//删除文件unlink($path);returntrue;}}/***CreatedbyPhpStorm.*function:uploadFile*Description:多文件上传*User:Xiaoxie*Email736214763@qq.com*@paramarray$files*@parambool$type*@returnbool|void**/publicfunctionuploadFile($files=[],$type=true){if(is_array($files)){foreach($filesas$key=>$file){if($type){$this->dir_mkdirs($file);}$this->off=@ftp_put($this->ftp,$file,$key,FTP_BINARY);if(!$this->off){logs('ftp.txt',date('Y-m-dH:i:s').$file."文件上传错误");}else{//删除文件unlink($key);//returntrue;}}}if(!$this->off){//logs函数自定义日志logs('ftp.txt',date('Y-m-dH:i:s').$file."文件上传错误");returnfalse;}else{returntrue;}}/***CreatedbyPhpStorm.*function:move_file*Description:移动文件修改文件名*User:Xiaoxie*Email736214763@qq.com*@param$path原路径*@param$newpath若目标目录不存在则新建*@parambool$type**/functionmove_file($path,$newpath,$type=true){if($type)$this->dir_mkdirs($newpath);$this->off=@ftp_rename($this->ftp,$path,$newpath);if(!$this->off){return"文件移动失败,请检查权限及原路径是否正确!";}else{returntrue;}}/***CreatedbyPhpStorm.*function:copy_file*Description:复制文件*User:Xiaoxie*Email736214763@qq.com*@param$path原路径*@param$newpath新路径*@parambool$type若目标目录不存在则新建**/functioncopy_file($path,$newpath,$type=true){$downpath="/var/www/temp.txt";$this->off=@ftp_get($this->ftp,$downpath,$path,FTP_BINARY);//下载if(!$this->off){return"文件复制失败,请检查权限及原路径是否正确!";}$this->up_file($downpath,$newpath,$type);}/***CreatedbyPhpStorm.*function:del_file*Description:删除文件*User:Xiaoxie*Email736214763@qq.com*@param$path**/functiondel_file($path){$this->off=@ftp_delete($this->ftp,$path);if(!$this->off){returnfalse;}}/***CreatedbyPhpStorm.*function:dir_mkdirs*Description:生成目录*User:Xiaoxie*Email736214763@qq.com*@param$path路径**/functiondir_mkdirs($path){$path_arr=explode('/',$path);//取目录数组$file_name=array_pop($path_arr);//弹出文件名$path_div=count($path_arr);//取层数foreach($path_arras$val)//创建目录{if(@ftp_chdir($this->ftp,$val)==FALSE){$tmp=@ftp_mkdir($this->ftp,$val);if($tmp==FALSE){exit;}@ftp_chdir($this->ftp,$val);}}for($i=1;$i<=$path_div;$i++)//回退到根{@ftp_cdup($this->ftp);}}/***CreatedbyPhpStorm.*function:close*Description:关闭链接*User:Xiaoxie*Email736214763@qq.com**/publicfunctionclose(){@ftp_close($this->ftp);}/***关闭链接*单例模式打开析构方法*/publicfunction__destruct(){//TODO:Implement__destruct()method.//@ftp_close($this->ftp);}}


lavarel中直接调用;

单例模式调用:

FtpFile::getInstance($this->data)->up_file($location_file,$remote_file);

不是单例模式调用;

$ftp=newFtpFile($this->data);$ftp->uploadFile($filearr);$ftp->close();