Back to FtpClient class

Method write

public bool
write
(mixed $remote, mixed $buffer)
Method to write a string to the FTP server
Parameters
  • string $remote FTP path to file to write to
  • string $buffer Contents to write to the FTP server
Returns
  • bool True if successful
Since
  • 1.5
Class: FtpClient
Project: Joomla

Method write - Source code

/**
 * Method to write a string to the FTP server
 *
 * @param   string  $remote  FTP path to file to write to
 * @param   string  $buffer  Contents to write to the FTP server
 *
 * @return  boolean  True if successful
 *
 * @since   1.5
 */
public function write($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+');
        fwrite($tmp, $buffer);
        rewind($tmp);
        if (@ftp_fput($this->_conn, $remote, $tmp, $mode) === false) {
            fclose($tmp);
            Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_BAD_RESPONSE', __METHOD__), Log::WARNING, 'jerror');
            return false;
        }
        fclose($tmp);
        return true;
    }
    // First we need to set the transfer mode
    $this->_mode($mode);
    // Start passive mode
    if (!$this->_passive()) {
        Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_PASSIVE', __METHOD__), Log::WARNING, 'jerror');
        return false;
    }
    // Send store command to the FTP server
    if (!$this->_putCmd('STOR ' . $remote, array(150, 125))) {
        Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_NOT_EXPECTED_RESPONSE_150_125', __METHOD__, $this->_response, $remote), Log::WARNING, 'jerror');
        @fclose($this->_dataconn);
        return false;
    }
    // Write buffer to the data connection port
    do {
        if (($result = @fwrite($this->_dataconn, $buffer)) === false) {
            Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_DATA_PORT', __METHOD__), Log::WARNING, 'jerror');
            return false;
        }
        $buffer = substr($buffer, $result);
    } while ($buffer != '');
    // Close the data connection port [Data transfer complete]
    fclose($this->_dataconn);
    // Verify that the server received the transfer
    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;
}