Back to FtpClient class

Method read

public bool
read
(mixed $remote, mixed &$buffer)
Method to read a file from the FTP server's contents into a buffer
Parameters
  • string $remote Path to remote file to read on the FTP server
  • string & $buffer Buffer variable to read file contents into
Returns
  • bool True if successful
Since
  • 1.5
Class: FtpClient
Project: Joomla

Method read - Source code

/**
 * Method to read a file from the FTP server's contents into a buffer
 *
 * @param   string  $remote   Path to remote file to read on the FTP server
 * @param   string  &$buffer  Buffer variable to read file contents into
 *
 * @return  boolean  True if successful
 *
 * @since   1.5
 */
public function read($remote, &$buffer)
{
    // 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;
        }
        $tmp = fopen('buffer://tmp', 'br+');
        if (@ftp_fget($this->_conn, $tmp, $remote, $mode) === false) {
            fclose($tmp);
            Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_BAD_RESPONSE', __METHOD__), Log::WARNING, 'jerror');
            return false;
        }
        // Read tmp buffer contents
        rewind($tmp);
        $buffer = '';
        while (!feof($tmp)) {
            $buffer .= fread($tmp, 8192);
        }
        fclose($tmp);
        return true;
    }
    $this->_mode($mode);
    // 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
    $buffer = '';
    while (!feof($this->_dataconn)) {
        $buffer .= fread($this->_dataconn, 4096);
    }
    // Close the data port connection
    fclose($this->_dataconn);
    // Let's try to cleanup some line endings if it is ascii
    if ($mode == FTP_ASCII) {
        $os = 'UNIX';
        if (IS_WIN) {
            $os = 'WIN';
        }
        $buffer = preg_replace('/' . CRLF . '/', $this->_lineEndings[$os], $buffer);
    }
    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;
}