Giter VIP home page Giter VIP logo

yii2-ftp's Introduction

FTP Client for Yii 2


Latest Stable Version Total Downloads License

yii2-ftp is a fork of Nicolab/php-ftp-client v1.2.0

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yii2mod/yii2-ftp "*"

or add

"yii2mod/yii2-ftp": "*"

to the require section of your composer.json.

Getting Started

Connect to a server FTP :

$ftp = new \yii2mod\ftp\FtpClient();
$host = 'ftp.example.com';
$ftp->connect($host);
$ftp->login($login, $password);

OR

Connect to a server FTP via SSL (on port 22 or other port) :

$ftp = new \yii2mod\ftp\FtpClient();
$host = 'ftp.example.com';
$ftp->connect($host, true, 22);
$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->count();

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

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

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

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

// count only the "symbolic links" in a given directory
$total_link = $ftp->count('/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.

Support us

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

yii2-ftp's People

Contributors

bbrunekreeft avatar cpuche avatar disem avatar dmitry-semenov avatar morontt avatar nicolab avatar pozitronik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

yii2-ftp's Issues

List hidden files in scanDir()

It seems that scanDir() do not request hidden files and folders (whose names started from .).
My suggestion: add parameter to scanDir() to include hidden items.

Does not work when i ssh

I tried using below.....but no luck

    $ftp = new \yii2mod\ftp\FtpClient();
    $host = 'xxx.xxx.xx.xx';
    $ftp->connect($host, true, 22222, 120);
    $ftp->login("xyz", "abcdef");

but was unable to connect...!!!!

Any help would be much appreciated.

This is a fork, please do not delete the original author of this lib

Please, be Fair-play...
Your project is a fork of https://github.com/Nicolab/php-ftp-client, you dont have right to remove the initial author name and put yours in the place as if you are the author of this. So I ask you to put a gain the name of the original author.

Please, append your name in the license if you want but do not delete the original author and the mention of the fork.

By fair-play and also for the users to know the history and parallel changes.

My PR #16 restore the original mentions, except the author section in the bottom of the original README.

Thanks

ftpClient mkdir

        /*
         * Path is '/srv/clips/1/5/0/5/7/vid/059468/arbm'
         * Explode is
                array (
                  0 => '',
                  1 => 'srv',
                  2 => 'clips',
                  3 => '1',
                  4 => '5',
                  5 => '0',
                  6 => '5',
                  7 => '7',
                  8 => 'vid',
                  9 => '059468',
                  10 => 'arbm',
                )
         */
        foreach ($parts as $part) {
            
            $part = $part=='' ? '/' : $part;
            ...

I had to add the last line, since chdir('') fails.

If I send target without leading slash, it the folder gets created under the current root.

In retrospect it seems obvious: you want me to chdir to '/srv' then mkdir.

But I am calling putAll(), and I don't necessarily know what the root/leading portion is.

Is there any problem with this suggested fix?

add stable tag

Would be nice to have a stable version in the repo, eg. 0.1.0

ftp_close(): 114 is not a valid FTP Buffer resource

ERROR:

PHP Warning – yii\base\ErrorException

ftp_close(): 114 is not a valid FTP Buffer resource


6. in C:\xampp\htdocs\myapp\vendor\yii2mod\yii2-ftp\FtpClient.php at line 76 –
An Error occurred while handling another error:
exception 'ReflectionException' with message 'Method yii2mod\ftp\FtpWrapper::close() does not exist' in C:\xampp\htdocs\myapp\vendor\yiisoft\yii2\web\ErrorHandler.php:199
Stack trace:
#0 C:\xampp\htdocs\myapp\vendor\yiisoft\yii2\web\ErrorHandler.php(199): ReflectionMethod->__construct('yii2mod\\ftp\\Ftp...', 'close')
#1 C:\xampp\htdocs\myapp\vendor\yiisoft\yii2\views\errorHandler\callStackItem.php(26): yii\web\ErrorHandler->addTypeLinks('yii2mod\\ftp\\Ftp...')
#2 C:\xampp\htdocs\myapp\vendor\yiisoft\yii2\web\ErrorHandler.php(249): require('C:\\xampp\\htdocs...')
#3 C:\xampp\htdocs\myapp\vendor\yiisoft\yii2\web\ErrorHandler.php(308): yii\web\ErrorHandler->renderFile('@yii/views/erro...', Array)
#4 C:\xampp\htdocs\myapp\vendor\yiisoft\yii2\views\errorHandler\exception.php(385): yii\web\ErrorHandler->renderCallStackItem('C:\\xampp\\htdocs...', 76, 'yii2mod\\ftp\\Ftp...', 'close', Array, 6)
#5 C:\xampp\htdocs\myapp\vendor\yiisoft\yii2\web\ErrorHandler.php(249): require('C:\\xampp\\htdocs...')
#6 C:\xampp\htdocs\myapp\vendor\yiisoft\yii2\web\ErrorHandler.php(114): yii\web\ErrorHandler->renderFile('@yii/views/erro...', Array)
#7 C:\xampp\htdocs\myapp\vendor\yiisoft\yii2\base\ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\base\ErrorException))
#8 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\base\ErrorException))
#9 {main}
Previous exception:
exception 'yii\base\ErrorException' with message 'ftp_close(): 114 is not a valid FTP Buffer resource' in C:\xampp\htdocs\myapp\vendor\yii2mod\yii2-ftp\FtpWrapper.php:79
Stack trace:
#0 [internal function]: yii\base\ErrorHandler->handleError(2, 'ftp_close(): 11...', 'C:\\xampp\\htdocs...', 79, Array)
#1 [internal function]: ftp_close(Resource id #114)
#2 C:\xampp\htdocs\myapp\vendor\yii2mod\yii2-ftp\FtpWrapper.php(79): call_user_func_array('ftp_close', Array)
#3 C:\xampp\htdocs\myapp\vendor\yii2mod\yii2-ftp\FtpClient.php(76): yii2mod\ftp\FtpWrapper->__call('close', Array)
#4 C:\xampp\htdocs\myapp\vendor\yii2mod\yii2-ftp\FtpClient.php(76): yii2mod\ftp\FtpWrapper->close()
#5 [internal function]: yii2mod\ftp\FtpClient->__destruct()
#6 [internal function]: app\modules\inventory\controllers\SyndicationController->actionProcess()
#7 C:\xampp\htdocs\myapp\vendor\yiisoft\yii2\base\InlineAction.php(57): call_user_func_array(Array, Array)
#8 C:\xampp\htdocs\myapp\vendor\yiisoft\yii2\base\Controller.php(156): yii\base\InlineAction->runWithParams(Array)
#9 C:\xampp\htdocs\myapp\vendor\yiisoft\yii2\base\Module.php(523): yii\base\Controller->runAction('process', Array)
#10 C:\xampp\htdocs\myapp\vendor\yiisoft\yii2\web\Application.php(102): yii\base\Module->runAction('inventory/syndi...', Array)
#11 C:\xampp\htdocs\myapp\vendor\yiisoft\yii2\base\Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
#12 C:\xampp\htdocs\myapp\web\index.php(13): yii\base\Application->run()
#13 {main}

My Code:

   $ftp = new \yii2mod\ftp\FtpClient(); 
    $host = '192.168.1.70';
    $login = 'xxxxxx';
    $password = 'xxxxx'; 
    $ftp->connect($host);
    $ftp->login($login, $password);
    $source_directory = Yii::$app->basePath . '/web/uploads/folder1/';
    $file = 'sample.csv';
    $target_directory = Yii::$app->basePath ."/web/uploads/test/";
    $ftp->put($file , $source_directory.$file,FTP_BINARY);
    $ftp->close(); 

New release?

Is it possible to create a new release?
There are 15 commits after the latest release, and some are resolving issues I am looking at.

Scandir not working if file contains spaces

On my ftp I have a file named "20160416194445-Greenscreen studio.xml"

The scandir function returns a file with the name "20160416194445-Greenscreen"

The ftp_rawlist command returns this:
-rw-r--r-- 1 0 0 188 Apr 15 15:46 20160415174629-Greenscreen studio.xml

In FTPClient parseRawList you should concatenate chunk 8->n to ensure you get the full name.

Require php-ftp extension

Since this package requires the PHP FTP extension, eg. here, it should be listed in composer.json.

I think it would be php-ftp, but I am not 100% sure, also not about the version.
You could also require yiisoft/yii2 btw.

PS: The extension is working really great!

Error ftp_close(): SSL read failed on most functions

Hi,
If I run certain actions like putAll, I get the above error:
ftp_close(): SSL read failed

On the contrast, if I loop through the files one by one and do a single ftp->put then it works,
but I have to reconnect for every file, otherwise the same error above comes.

Any ideas? I am ftping into a CDN server, so there is nothing I could change on the CDN side.

Thanks for any ideas...
gb5256

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.