概要
PHPでFTPサーバにファイルを転送する基本をまとめておく。
基本サンプル
<?php
$ftpServer = 'XXXXX.com';
$ftpUserName = 'XXXXX';
$ftpUserPass = 'XXXXX';
$localFile = '/Users/Test/test.txt'; //転送したいファイルを書く
$remoteFile = '/home/XXXXX/Test/test.txt'; //転送先での名前を指定する
// 接続を確立する
$conId = ftp_connect($ftpServer);
// ユーザー名とパスワードでログインする
$loginResult = ftp_login($conId, $ftpUserName, $ftpUserPass);
// ファイルをアップロードする
if (ftp_put($conId, $remoteFile, $localFile, FTP_ASCII)) {
echo "successfully uploaded $localFile\n";
} else {
echo "There was a problem while uploading $localFile\n";
}
// 接続を閉じる
ftp_close($conId);
?>