• Stars
    star
    632
  • Rank 71,124 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 10 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A flexible FTP and SSL-FTP client for PHP

nicolab/php-ftp-client

A flexible FTP and SSL-FTP client for PHP. This lib provides helpers easy to use to manage the remote files.

This package is aimed to remain simple and light. It's only a wrapper of the FTP native API of PHP, with some useful helpers. If you want to customize some methods, you can do this by inheriting one of the 3 classes of the package.

Install

  • Install package with composer
composer require nicolab/php-ftp-client
  • Or use GIT clone command:
git clone [email protected]:Nicolab/php-ftp-client.git
  • Or download the library, configure your autoloader or include the 3 files of php-ftp-client/src/FtpClient directory.

Getting Started

Connect to a server FTP :

$ftp = new \FtpClient\FtpClient();
$ftp->connect($host);
$ftp->login($login, $password);

OR

Connect to a server FTP via SSL (on port 990 or another port) :

$ftp = new \FtpClient\FtpClient();
$ftp->connect($host, true, 990);
$ftp->login($login, $password);

Note: The connection is implicitly closed at the end of script execution (when the object is destroyed). Therefore it is unnecessary to call $ftp->close(), except for an explicit re-connection.

Usage

Upload all files and all directories is easy :

// upload with the BINARY mode
$ftp->putAll($source_directory, $target_directory);

// Is equal to
$ftp->putAll($source_directory, $target_directory, FTP_BINARY);

// or upload with the ASCII mode
$ftp->putAll($source_directory, $target_directory, FTP_ASCII);

Note : FTP_ASCII and FTP_BINARY are predefined PHP internal constants.

Get a directory size :

// size of the current directory
$size = $ftp->dirSize();

// size of a given directory
$size = $ftp->dirSize('/path/of/directory');

Count the items in a directory :

// count in the current directory
$total = $ftp->countItems();
// or alias
$total = $ftp->count();

// count in a given directory
$total = $ftp->countItems('/path/of/directory');

// count only the "files" in the current directory
$total_file = $ftp->countItems('.', 'file');

// count only the "files" in a given directory
$total_file = $ftp->countItems('/path/of/directory', 'file');

// count only the "directories" in a given directory
$total_dir = $ftp->countItems('/path/of/directory', 'directory');

// count only the "symbolic links" in a given directory
$total_link = $ftp->countItems('/path/of/directory', 'link');

Detailed list of all files and directories :

// scan the current directory and returns the details of each item
$items = $ftp->scanDir();

// scan the current directory (recursive) and returns the details of each item
var_dump($ftp->scanDir('.', true));

Result:

'directory#www' =>
    array (size=10)
      'permissions' => string 'drwx---r-x' (length=10)
      'number'      => string '3' (length=1)
      'owner'       => string '32385' (length=5)
      'group'       => string 'users' (length=5)
      'size'        => string '5' (length=1)
      'month'       => string 'Nov' (length=3)
      'day'         => string '24' (length=2)
      'time'        => string '17:25' (length=5)
      'name'        => string 'www' (length=3)
      'type'        => string 'directory' (length=9)

  'link#www/index.html' =>
    array (size=11)
      'permissions' => string 'lrwxrwxrwx' (length=10)
      'number'      => string '1' (length=1)
      'owner'       => string '0' (length=1)
      'group'       => string 'users' (length=5)
      'size'        => string '38' (length=2)
      'month'       => string 'Nov' (length=3)
      'day'         => string '16' (length=2)
      'time'        => string '14:57' (length=5)
      'name'        => string 'index.html' (length=10)
      'type'        => string 'link' (length=4)
      'target'      => string '/var/www/shared/index.html' (length=26)

'file#www/README' =>
    array (size=10)
      'permissions' => string '-rw----r--' (length=10)
      'number'      => string '1' (length=1)
      'owner'       => string '32385' (length=5)
      'group'       => string 'users' (length=5)
      'size'        => string '0' (length=1)
      'month'       => string 'Nov' (length=3)
      'day'         => string '24' (length=2)
      'time'        => string '17:25' (length=5)
      'name'        => string 'README' (length=6)
      'type'        => string 'file' (length=4)

All FTP PHP functions are supported and some improved :

// Requests execution of a command on the FTP server
$ftp->exec($command);

// Turns passive mode on or off
$ftp->pasv(true);

// Set permissions on a file via FTP
$ftp->chmod(0777, 'file.php');

// Removes a directory
$ftp->rmdir('path/of/directory/to/remove');

// Removes a directory (recursive)
$ftp->rmdir('path/of/directory/to/remove', true);

// Creates a directory
$ftp->mkdir('path/of/directory/to/create');

// Creates a directory (recursive),
// creates automaticaly the sub directory if not exist
$ftp->mkdir('path/of/directory/to/create', true);

// and more ...

Get the help information of remote FTP server :

var_dump($ftp->help());

Result :

array (size=6)
  0 => string '214-The following SITE commands are recognized' (length=46)
  1 => string ' ALIAS' (length=6)
  2 => string ' CHMOD' (length=6)
  3 => string ' IDLE' (length=5)
  4 => string ' UTIME' (length=6)
  5 => string '214 Pure-FTPd - http://pureftpd.org/' (length=36)

Note : The result depend of FTP server.

Extend

Create your custom FtpClient.

// MyFtpClient.php

/**
 * My custom FTP Client
 * @inheritDoc
 */
class MyFtpClient extends \FtpClient\FtpClient {

  public function removeByTime($path, $timestamp) {
      // your code here
  }

  public function search($regex) {
      // your code here
  }
}
// example.php
$ftp = new MyFtpClient();
$ftp->connect($host);
$ftp->login($login, $password);

// remove the old files
$ftp->removeByTime('/www/mysite.com/demo', time() - 86400);

// search PNG files
$ftp->search('/(.*)\.png$/i');

API doc

See the source code for more details. It is fully documented πŸ“˜

Testing

Tested with "atoum" unit testing framework.

License

MIT c) 2014, Nicolas Tallefourtane.

Author

Nicolas Tallefourtane - Nicolab.net
Nicolas Talle
Make a donation via Paypal

More Repositories

1

atom-local-history

Atom package for maintaining local history of files.
JavaScript
77
star
2

crystal-validator

πŸ’Ž Data validation module for Crystal lang
Crystal
29
star
3

evemit

Minimal and fast JavaScript event emitter for Node.js and front-end (only 1kb)
JavaScript
15
star
4

crystal-dbx

ORM and query builder for Crystal lang.
Crystal
15
star
5

crystal-result

πŸ’Ž Rust-like error handling for Crystal (`Ok` / `Err`)
Crystal
9
star
6

atom-package-js-generator

[No longer maintained] Generate Atom.io packages in Javascript instead of CoffeeScript.
JavaScript
8
star
7

crystal-crypt

πŸ’Ž Cryptographic utilities made easy for Crystal lang.
Crystal
7
star
8

storux

Easy and powerful state store manager.
JavaScript
7
star
9

crystal-lru-cache

πŸ’Ž key/value LRU cache that supports lifecycle, global size limit and expiration time.
Crystal
7
star
10

node-ipc-events

Inter process (IPC) event emitter
JavaScript
6
star
11

gulp-if-else

[Gulp plugin] Conditional task with "if" callback and "else" callback (optional) : gulp.src(source).pipe( ifElse(condition, ifCallback, elseCallback) )
JavaScript
6
star
12

eslint-config-common

An ESLint Shareable Config for Common JavaScript Coding style.
JavaScript
5
star
13

crystal-prop

Properties utilities for Crystal lang (improved getter, IoC, factory, ...).
Crystal
5
star
14

routux

A fast and productive router to improve the UX (User eXperience) of any front-end application (supports middlewares, regex pattern, named routes, error handler, ...).
JavaScript
5
star
15

mongoose-tags

A simple tagging plugin for Mongoose
JavaScript
4
star
16

granite-paginate

Crystal shard adding pagination support for Granite ORM.
Crystal
3
star
17

envlist

envlist is a micro-module (without dependency) for resolving the type of runtime environment between your application convention and another convention (like NODE_ENV).
JavaScript
3
star
18

waitwait

Golang's `WaitGroup` and Unix's `sleep` for Javascript (browser and Node.js).
JavaScript
2
star
19

goflow

A base workflow for Golang projects: Docker + Golang + inspiration from standards.
Shell
2
star
20

crystal-testify

Testing utilities for Crystal lang specs. OOP abstraction for creating unit and integration tests.
Crystal
2
star
21

qfiles.js

Helpers for handling files with Node.js, without dependencies (requireAll, requireToObj, RequireFiles, ...).
JavaScript
2
star
22

Nicolab

1
star
23

node-spawn-handler

Simple handler for ChildProcess.spawn of Node.js
JavaScript
1
star
24

eslint-config-common-jsx

An ESLint Shareable Config for JSX support in JavaScript Common coding Style
JavaScript
1
star
25

node-error-render

[Node.js] Prettify the details of the errors in the console
JavaScript
1
star
26

eslint-config-common-react

An ESLint Shareable Config for React/JSX support in JavaScript Common coding Style
JavaScript
1
star
27

core-stack

The base of a Javascript core object with builtin: plugin system, event emitter and stack handler.
JavaScript
1
star
28

atom-helpers

A Node.JS package that provides helpers for Atom.io packages development
JavaScript
1
star
29

rust-guess-game

Κ˜β€ΏΚ˜ β–² A little guessing game developed in Rust for fun and discovery β–Ό Κ˜β€ΏΚ˜
Rust
1
star
30

node-spawn-rmrf

Removes recursively with rm -rf `./file/path` (spawned).
JavaScript
1
star