一:网上找的代码。测试后发现有些地方不对,可能是PHP版本的问题。后来改进下(我是PHP7)
切记 php.ini :开启extension=php_ftp.dll 扩展
<?php
namespace Ftp;
use think\Config;
/**
* Ftp文件移动工具
*/
class ftp_tool
{
// 返回操作状态(成功/失败)
public $off;
// FTP连接
public $conn_id;
// FPT IP
protected $ftp_host = '';
// FTP 端口
protected $ftp_port = '';
// FTP 帐号
protected $ftp_user = '';
// FTP 密码
protected $ftp_pass = '';
// FTP 上传路径
protected $ftp_path = '';
/**
* 构造方法
*/
public function __construct()
{
// 获取配置文件
$ftp_config = Config::get('ftp');
$this->ftp_host = $ftp_config['ftp_host'];
$this->ftp_user = $ftp_config['ftp_user'];
$this->ftp_port = $ftp_config['ftp_port'];
$this->ftp_pass = $ftp_config['ftp_pass'];
$this->ftp_path = $ftp_config['ftp_path'];
// 建立连接
$this->connection($this->ftp_host, $this->ftp_user, $this->ftp_pass);
}
/**
* 建立连接
* @param $ftp_host --ftp地址
* @param $ftp_user --ftp用户名
* @param $ftp_pass --ftp密码
* @return bool
*/
public function connection($ftp_host, $ftp_user, $ftp_pass)
{
$this->conn_id = ftp_connect($ftp_host) or die('连接失败');
if (! @ftp_login($this->conn_id, $ftp_user, $ftp_pass)){
return false;
}
// @ftp_pasv($this->conn_id,1); // 打开被动模拟
// 进入指定的目录(进入指定目录报错,具体原因未排除)
// ftp_chdir($this->conn_id,$this->ftp_path);
}
/**
* 方法:上传文件
* @param $file_url -- 上传图全路径
* @param $file_name -- 上传图文件名
* @return string
*/
public function up_file($file_url, $file_name)
{
// ftp 文件夹+上传文件名
$pic_name = $this->ftp_path . $file_name;
$this->off = ftp_put($this->conn_id, $pic_name, $file_url, FTP_BINARY);
if (!$this->off){
return false; //"文件上传失败,请检查权限及路径是否正确!"
}
return true;
}
/**
* 方法:移动文件
* @path-- 原路径
* @newpath -- 新路径
* @type-- 若目标目录不存在则新建
* @param $path
* @param $newpath
* @param bool $type
* @return string
*/
public function move_file($path, $newpath, $type = true)
{
if ($type) $this->dir_mkdirs($newpath);
$this->off = @ftp_rename($this->conn_id, $path, $newpath);
if (!$this->off){
return false; //"文件移动失败,请检查权限及原路径是否正确!"
}
return true;
}
/**
* 方法:复制文件
* 说明:由于FTP无复制命令,本方法变通操作为:下载后再上传到新的路径
* @path-- 原路径
* @newpath -- 新路径
* @type-- 若目标目录不存在则新建
* @param $path
* @param $newpath
* @param bool $type
* @return string
*/
public function copy_file($path, $newpath, $type = true)
{
$downpath = "c:/tmp.dat";
$this->off = @ftp_get($this->conn_id, $downpath, $path, FTP_BINARY);// 下载
if (!$this->off){
return false; //"文件复制失败,请检查权限及原路径是否正确!"
}
$this->up_file($downpath, $newpath, $type);
return true;
}
/**
* 方法:删除文件
* @path -- 路径
* @param $path
* @return string
*/
public function del_file($path)
{
$path = $this->ftp_path . $path;
$this->off = @ftp_delete($this->conn_id, $path);
if (!$this->off){
return $this->off; //"文件删除失败,请检查权限及路径是否正确!"
}
return $this->off;
}
/**
* 方法:生成目录
* @path -- 路径
* @param $path
* @return string
*/
public 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->conn_id, $val) == FALSE) {
$tmp = @ftp_mkdir($this->conn_id, $val);
if ($tmp == FALSE) {
return false; //"目录创建失败,请检查权限及路径是否正确!"
}
@ftp_chdir($this->conn_id, $val);
}
}
for ($i = 1; $i <= $path_div; $i++) // 回退到根
{
@ftp_cdup($this->conn_id);
}
return true;
}
/**
* 方法:关闭FTP连接
*/
public function close()
{
@ftp_close($this->conn_id);
}
}