Files
lsky-pro/extend/strategy/driver/Remote.php
T
2020-02-29 23:38:33 +08:00

130 lines
2.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* Created by WispX.
* User: WispX <1591788658@qq.com>
* Date: 2019/10/31
* Time: 9:39 上午
* Link: https://github.com/wisp-x
*/
namespace strategy\driver;
use FtpClient\FtpException;
use strategy\Driver;
class Remote implements Driver
{
private $ftp;
private $error;
public function __construct($options = [])
{
try {
$this->ftp = new \FtpClient\FtpClient();
$this->ftp->connect($options['remote_host']);
$this->ftp = $this->ftp->login($options['remote_name'], $options['remote_password']);
} catch (FtpException $e) {
$this->error = $e->getMessage();
}
}
/**
* 创建文件
*
* @param $pathname
* @param $file
*
* @return mixed
*/
public function create($pathname, $file)
{
if (!$this->check()) return false;
try {
$dirname = dirname($pathname);
if (!$this->ftp->isDir($dirname)) {
if (!$this->ftp->mkdir($dirname, true)) {
throw new FtpException('文件夹创建失败!');
}
}
if (!$this->ftp->put($pathname, $file, FTP_BINARY)) {
throw new FtpException('上传失败');
}
} catch (FtpException $e) {
$this->error = $e->getMessage();
return false;
}
return true;
}
/**
* 删除单个文件
*
* @param $pathname
*
* @return mixed
*/
public function delete($pathname)
{
if (!$this->check()) return false;
try {
if (!$this->ftp->remove($pathname, true)) {
throw new FtpException('删除失败');
}
} catch (FtpException $e) {
$this->error = $e->getMessage();
return false;
}
return true;
}
/**
* 删除多个文件
*
* @param array $list 一维数组
*
* @return mixed
*/
public function deletes(array $list)
{
if (!$this->check()) return false;
try {
foreach ($list as $item) {
if (!$this->ftp->remove($item, true)) {
throw new FtpException('删除失败');
}
}
} catch (FtpException $e) {
$this->error = $e->getMessage();
return false;
}
return true;
}
/**
* 获取出错信息
*
* @return mixed
*/
public function getError()
{
return 'Remote' . $this->error;
}
private function check()
{
if (!extension_loaded('ftp')) {
$this->error = 'php_ftp 拓展未开启';
return false;
}
return true;
}
}