Back to FtpClient class

Method store

public bool
store
(mixed $local, mixed $remote = null)
Method to store a file to the FTP server
Parameters
  • string $local Path to local file to store on the FTP server
  • string $remote FTP path to file to create
Returns
  • bool True if successful
Since
  • 1.5
Class: FtpClient
Project: Joomla

Method store - Source code

/**
 * Method to store a file to the FTP server
 *
 * @param   string  $local   Path to local file to store on the FTP server
 * @param   string  $remote  FTP path to file to create
 *
 * @return  boolean  True if successful
 *
 * @since   1.5
 */
public function store($local, $remote = null)
{
    // If remote file is not given, use the filename of the local file in the current
    // working directory.
    if ($remote == null) {
        $remote = basename($local);
    }
    // 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_put($this->_conn, $remote, $local, $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 exists and if so open it for reading
    if (@file_exists($local)) {
        $fp = fopen($local, 'rb');
        if (!$fp) {
            Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_LOCAL_FILE_OPEN_READING', __METHOD__, $local), Log::WARNING, 'jerror');
            return false;
        }
    } else {
        Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_LOCAL_FILE_FIND', __METHOD__, $local), Log::WARNING, 'jerror');
        return false;
    }
    // Start passive mode
    if (!$this->_passive()) {
        @fclose($fp);
        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))) {
        @fclose($fp);
        @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;
    }
    // Do actual file transfer, read local file and write to data port connection
    while (!feof($fp)) {
        $line = fread($fp, 4096);
        do {
            if (($result = @fwrite($this->_dataconn, $line)) === false) {
                Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_DATA_PORT', __METHOD__), Log::WARNING, 'jerror');
                return false;
            }
            $line = substr($line, $result);
        } while ($line != '');
    }
    fclose($fp);
    fclose($this->_dataconn);
    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;
}