PHPでFTPファイルアップロード / ダウンロード を行うには PHPのFTP関数 http://php.net/ftp を使用する方法がありますが、
https://packagist.org/packages/league/flysystem
を使用するほうが早く、FTPをはじめとする次の転送方式にも同じインターフェイスで対応できます(要 追加インストール)
composer からインストールします
composer require league/flysystem
autoload.php を読みこませればOK
autoloadを使いたくない場合は次のように spl_autoload_register() に無名関数を登録してオートロードを作成します。
spl_autoload_register(function($class) {
$prefix = 'League\\Flysystem\\';
if ( ! substr($class, 0, 17) === $prefix) {
return;
}
$class = substr($class, strlen($prefix));
$location = __DIR__ . 'path/to/flysystem/src/' . str_replace('\\', '/', $class) . '.php';
if (is_file($location)) {
require_once($location);
}
});
(ローカル)
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local as Adapter;
$filesystem = new Filesystem(new Adapter('/path/to/directory'));
(FTP)
use League\Flysystem\Adapter\Sftp as Adapter;
$filesystem = new Filesystem(new Adapter(array(
'host' => 'example.com',
'port' => 21,
'username' => 'username',
'password' => 'password',
'privateKey' => 'path/to/or/contents/of/privatekey',
'root' => '/path/to/root',
'timeout' => 10,
)));
// ファイルの存在確認
$exists = $filesystem->has('path/to/file.txt');
// ファイルを読み込む
$contents = $filesystem->read('path/to/file.txt');
// ファイルを読み込むストリームを得る
$stream = $filesystem->readStream('something/is/here.ext');
// ファイルに書き込む
$filesystem->write('path/to/file.txt', 'contents');
// ファイルに追記する
$filesystem->put('path/to/file.txt', 'contents');
// ファイルを削除する
$filesystem->delete('path/to/file.txt');
// ファイルを読み込んで削除する
$contents = $filesystem->readAndDelete('path/to/file.txt');
// ファイルをリネーム(移動)する
$filesystem->rename('filename.txt', 'newname.txt');
Fatal error: Call to undefined function League\Flysystem\Adapter\ftp_connect() in vendor/league/flysystem/src/Adapter/Ftp.php on line 98
この場合はPHPのftp関数が使用できないサーバですので使用できません。 ソケット通信を使用したFTPクラスを使用するといいでしょう