Back to FtpClient class

Method get

public bool
get
(mixed $local, mixed $remote)
Method to get a file from the FTP server and save it to a local file
Parameters
  • string $local Local path to save remote file to
  • string $remote Path to remote file to get on the FTP server
Returns
  • bool True if successful
Since
  • 1.5
Class: FtpClient
Project: Joomla

Method get - Source code

/**
 * Method to get a file from the FTP server and save it to a local file
 *
 * @param   string  $local   Local path to save remote file to
 * @param   string  $remote  Path to remote file to get on the FTP server
 *
 * @return  boolean  True if successful
 *
 * @since   1.5
 */
public function get($local, $remote)
{
    // Determine file type
    $mode = $this->_findMode($remote);
    // If native FTP support is enabled let's use it...
    if (FTP_NATIVE) {
        // Turn passive mode on
        if (@ftp_pasv($this->_conn, true) === false) {
            Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_PASSIVE', __METHOD__), Log::WARNING, 'jerror');
            return false;
        }
        if (@ftp_get($this->_conn, $local, $remote, $mode) === false) {
            Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_BAD_RESPONSE', __METHOD__), Log::WARNING, 'jerror');
            return false;
        }
        return true;
    }
    $this->_mode($mode);
    // Check to see if the local file can be opened for writing
    $fp = fopen($local, 'wb');
    if (!$fp) {
        Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_LOCAL_FILE_OPEN_WRITING', __METHOD__, $local), Log::WARNING, 'jerror');
        return false;
    }
    // Start passive mode
    if (!$this->_passive()) {
        Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_PASSIVE', __METHOD__), Log::WARNING, 'jerror');
        return false;
    }
    if (!$this->_putCmd('RETR ' . $remote, array(150, 125))) {
        @fclose($this->_dataconn);
        Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_NOT_EXPECTED_RESPONSE_150_125', __METHOD__, $this->_response, $remote), Log::WARNING, 'jerror');
        return false;
    }
    // Read data from data port connection and add to the buffer
    while (!feof($this->_dataconn)) {
        $buffer = fread($this->_dataconn, 4096);
        fwrite($fp, $buffer, 4096);
    }
    // Close the data port connection and file pointer
    fclose($this->_dataconn);
    fclose($fp);
    if (!$this->_verifyResponse(226)) {
        Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_TRANSFER_FAILED', __METHOD__, $this->_response, $remote), Log::WARNING, 'jerror');
        return false;
    }
    return true;
}