您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
原文博客地址http://xgs888.top/post/view?id=97;
PHP针对ftp文件的操作方法,如果是只操作一个ftp,可以使用里面的单利模式,
不需要每次都去实例化,我的项目中需要去链接很多个ftp服务器;
所以需要多次去连接和关闭;
<?php /** * Created by PhpStorm. * ftp的文件操作类 * User: xiaoxie * Date: 2018/5/7 * Time: 17:44 **/ namespace App\Tools; class FtpFile { static private $_instance=null; private $ftp = null; public $off; // 返回操作状态(成功/失败) //私有的构造方法 public function __construct($config){ //实例化 $this->ftp = @ftp_connect($config['ftp_ip'],$config['ftp_port']) or die("FTP connection fail"); //登录验证 @ftp_login($this->ftp,$config['ftp_username'],$config['ftp_password']); //是否开启被动模式 if (isset($config['ftp_pasv'])) { @ftp_pasv($this->ftp,true); } } /** * Created by PhpStorm. * function: getInstance * Description:公有的静态方法 * User: Xiaoxie * Email 736214763@qq.com * @return FtpFile|null * */ static public function getInstance($config){ if(!(self::$_instance instanceof self)){ self::$_instance = new FtpFile($config); } return self::$_instance; } /** * Created by PhpStorm. * function: up_file * Description:上传文件 * User: Xiaoxie * Email 736214763@qq.com * @param $path 本地路径 * @param $newpath 若目标目录不存在则新建 * @param bool $type * */ function up_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); return true; } } /** * Created by PhpStorm. * function: uploadFile * Description:多文件上传 * User: Xiaoxie * Email 736214763@qq.com * @param array $files * @param bool $type * @return bool|void * */ public function uploadFile($files=[],$type=true) { if (is_array($files)) { foreach ($files as $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-d H:i:s').$file."文件上传错误"); }else{ //删除文件 unlink($key); // return true; } } } if(!$this->off) { //logs函数自定义日志 logs('ftp.txt',date('Y-m-d H:i:s').$file."文件上传错误"); return false; }else{ return true; } } /** * Created by PhpStorm. * function: move_file * Description:移动文件 修改文件名 * User: Xiaoxie * Email 736214763@qq.com * @param $path 原路径 * @param $newpath 若目标目录不存在则新建 * @param bool $type * */ function move_file($path,$newpath,$type=true) { if($type) $this->dir_mkdirs($newpath); $this->off = @ftp_rename($this->ftp,$path,$newpath); if(!$this->off) { return "文件移动失败,请检查权限及原路径是否正确!"; }else{ return true; } } /** * Created by PhpStorm. * function: copy_file * Description:复制文件 * User: Xiaoxie * Email 736214763@qq.com * @param $path原路径 * @param $newpath 新路径 * @param bool $type 若目标目录不存在则新建 * */ function copy_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); } /** * Created by PhpStorm. * function: del_file * Description:删除文件 * User: Xiaoxie * Email 736214763@qq.com * @param $path * */ function del_file($path) { $this->off = @ftp_delete($this->ftp,$path); if(!$this->off){ return false; } } /** * Created by PhpStorm. * function: dir_mkdirs * Description:生成目录 * User: Xiaoxie * Email 736214763@qq.com * @param $path 路径 * */ function dir_mkdirs($path) { $path_arr = explode('/',$path); // 取目录数组 $file_name = array_pop($path_arr); // 弹出文件名 $path_div = count($path_arr); // 取层数 foreach($path_arr as $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); } } /** * Created by PhpStorm. * function: close * Description:关闭链接 * User: Xiaoxie * Email 736214763@qq.com * */ public function close() { @ftp_close($this->ftp); } /** * 关闭链接 *单例模式打开析构方法 */ public function __destruct() { // TODO: Implement __destruct() method. //@ftp_close($this->ftp); } }
lavarel中直接调用;
单例模式调用:
FtpFile::getInstance($this->data)->up_file($location_file,$remote_file);
不是单例模式调用;
$ftp = new FtpFile($this->data); $ftp->uploadFile($filearr); $ftp->close();
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。