FTP file handler draft

This commit is contained in:
Dennis Eichhorn 2017-01-15 21:31:41 +01:00
parent ff2b3b0a5a
commit c477bb8ce4
3 changed files with 77 additions and 7 deletions

View File

@ -40,13 +40,54 @@ class File extends FileAbstract implements FileInterface
*/
public static function put(string $path, string $content, int $mode = ContentPutMode::REPLACE | ContentPutMode::CREATE) : bool
{
// todo: create all else cases, right now all getting handled the same way which is wrong
$current = ftp_pwd($con);
if(!ftp_chdir($con, File::dirpath($path))) {
return false;
}
$exists = self::exists($path);
$result = false;
if (
(($mode & ContentPutMode::APPEND) === ContentPutMode::APPEND && $exists)
|| (($mode & ContentPutMode::PREPEND) === ContentPutMode::PREPEND && $exists)
|| (($mode & ContentPutMode::REPLACE) === ContentPutMode::REPLACE && $exists)
|| (!$exists && ($mode & ContentPutMode::CREATE) === ContentPutMode::CREATE)
) {
if (!Directory::exists(dirname($path))) {
Directory::create(dirname($path), '0644', true);
}
$stream = fopen('data://temp,' . $content, 'r');
ftp_fput($path, $content);
fclose($stream);
$result = true;
}
ftp_chdir($current);
return $result;
}
/**
* {@inheritdoc}
*/
public static function get(string $path) : string
public static function get(string $path) : /* ? */string
{
$temp = fopen('php://temp', 'r+');
$current = ftp_pwd($con);
if(ftp_chdir($con, File::dirpath($path)) && ftp_fget($con, $temp, $path, FTP_BINARY, 0)) {
rewind($temp);
$content = stream_get_contents($temp);
}
fclose($temp);
ftp_chdir($current);
return $content ?? null;
}
/**
@ -78,7 +119,8 @@ class File extends FileAbstract implements FileInterface
*/
public static function exists(string $path) : bool
{
if(ftp_pwd($con) !== LocalFile::dirpath($path)) {
if(($current = ftp_pwd($con)) !== LocalFile::dirpath($path)) {
if(!ftp_chdir($con, $path)) {
return false;
}
@ -86,6 +128,8 @@ class File extends FileAbstract implements FileInterface
$list = ftp_nlist($con, $path);
ftp_chdir($con, $current);
return in_array($path, $list);
}
@ -94,6 +138,7 @@ class File extends FileAbstract implements FileInterface
*/
public static function parent(string $path) : string
{
return Directory::parent($path);
}
/**
@ -117,7 +162,7 @@ class File extends FileAbstract implements FileInterface
*/
public static function changed(string $path) : \DateTime
{
return ftp_mdtm($con, $path);
}
/**
@ -125,7 +170,11 @@ class File extends FileAbstract implements FileInterface
*/
public static function size(string $path, bool $recursive = true) : int
{
if (!self::exists($path)) {
throw new PathException($path);
}
return ftp_size($con, $path);
}
/**
@ -187,7 +236,20 @@ class File extends FileAbstract implements FileInterface
*/
public static function move(string $from, string $to, bool $overwrite = false) : bool
{
if (!self::exists($from)) {
throw new PathException($from);
}
if ($overwrite || !self::exists($to)) {
if (!Directory::exists(dirname($to))) {
Directory::create(dirname($to), '0644', true);
}
return ftp_rename($con, $from, $to);
}
return false;
}
/**
@ -195,7 +257,13 @@ class File extends FileAbstract implements FileInterface
*/
public static function delete(string $path) : bool
{
if (!self::exists($path)) {
return false;
}
ftp_delete($con, $path);
return true;
}
/**

View File

@ -34,12 +34,12 @@ class FtpStorage extends StorageAbstract
{
private $con = null;
public function __construct(string $uri, int $port = 21, string $login = null, string $pass = null, bool $ssl = false)
public function __construct(string $uri, int $port = 21, bool $mode = true, string $login = null, string $pass = null, bool $ssl = false)
{
$this->connect($uri, $port = 21, $login = null, $pass = null, $ssl = false);
$this->connect($uri, $port = 21, $mode, $login = null, $pass = null, $ssl = false);
}
public function connect(string $uri, int $port = 21, string $login = null, string $pass = null, bool $ssl = false) : bool
public function connect(string $uri, int $port = 21, bool $mode = true, string $login = null, string $pass = null, bool $ssl = false) : bool
{
if($ssl) {
$this->con = ftp_connect($uri, $port);
@ -47,6 +47,8 @@ class FtpStorage extends StorageAbstract
$this->con = ftp_ssl_connect($uri, $port);
}
ftp_pasv($this->con, $mode);
if(isset($login) && isset($pass)) {
ftp_login($this->con, $login, $pass);
}

View File

@ -463,7 +463,7 @@ class File extends FileAbstract implements FileInterface
*/
public static function basename(string $path) : string
{
// TODO: Implement basename() method.
return basename($path);
}
/**