YII Upload the file to FTP server

The below function used to upload the local file to remote directory.

[php]
/**
* Upload the file in ftp directory
* @param string $localFilePath
* @param string $remoteFilePath .
* @return boolean
*/
public function CopyFileToFtpBucketDir($localFilePath, $remoteFilePath)
{
<strong><em>//Credential are configured in main.php</em></strong>
$ftpSource = Yii::app()-&gt;params[‘ftpSource’];
$ftpUserName = Yii::app()-&gt;params[‘ftpUserName’];
$ftpPassword = Yii::app()-&gt;params[‘ftpPassword’];
<em><strong>// set up basic connection</strong></em>
$connect = ftp_connect($ftpSource);
if (!$connect)
{
Yii::log(‘ftp is not connected’, CLogger::LEVEL_ERROR, ‘some string to identify.’ . strstr(str_replace(‘/’, ‘.’, __DIR__), ‘some string to identify’) . ‘.’ . __METHOD__);
exit;
}
else
{
Yii::log(“Login with username and password”, CLogger::LEVEL_ERROR, ‘some string to identify.’ . strstr(str_replace(‘/’, ‘.’, __DIR__), ‘some string to identify’) . ‘.’ . __METHOD__);
$loginResult = ftp_login($connect, $ftpUserName, $ftpPassword);
<em><strong>// upload a file</strong></em>
if (ftp_put($connect, $remoteFilePath, $localFilePath, FTP_ASCII))
{
Yii::log(“Successfully uploaded $remoteFilePath\n”, CLogger::LEVEL_ERROR, ‘some string to identify.’ . strstr(str_replace(‘/’, ‘.’, __DIR__), ‘some string to identify’) . ‘.’ . __METHOD__);
$return = true;
}
else
{
Yii::log(“There was a problem while uploading $remoteFilePath\n”, CLogger::LEVEL_ERROR, ‘some string to identify.’ . strstr(str_replace(‘/’, ‘.’, __DIR__), ‘some string to identify’) . ‘.’ . __METHOD__);
$return = false;
}
<em><strong>// close the connection</strong></em>
ftp_close($connect);
return $return;
}
}
[/php]

Permanent link to this article: https://blog.openshell.in/2013/10/yii-upload-the-file-to-ftp-server/

Leave a Reply

Your email address will not be published.