_serverAddress = $serverAddress; $this->_username = $username; $this->_password = $password; } /** * Set the protocol, default is ftp * @example setProtoCol(CurlFtpUploader::PROTOCOL_SFTP); * @example setProtoCol(CurlFtpUploader::PROTOCOL_FTP); * @param string $protocol * @throws \Exception * @return \Utility\Remote\CurlFtpUploader */ public function setProtoCol($protocol) { if(self::PROTOCOL_FTP != $protocol && self::PROTOCOL_SFTP != $protocol) { throw new \Exception('Protocol must be CurlFtpUploader::PROTOCOL_FTP or CurlFtpUploader::PROTOCOL_SFTP'); } $this->_protocol = $protocol; if($protocol == self::PROTOCOL_SFTP) { $this->setPort(22); } return $this; } /** * Set the port, default is 21 * @example setPort(22); * @param int $port * @throws \Exception * @return \Utility\Remote\CurlFtpUploader */ public function setPort($port) { $port = (int) $port; if($port == 0) { throw new \Exception('Port must be an int and bigger than 0'); } $this->_port = $port; return $this; } /** * Set CURLOPT_SSL_VERIFYPEER, default is false * @param bool $value * @return \Utility\Remote\CurlFtpUploader */ public function setSslVerifyPeer($value) { $this->_sslVerifyPeer = (bool) $value; return $this; } /** * Set CURLOPT_SSL_VERIFYHOST, default is false * @param bool $value * @return \Utility\Remote\CurlFtpUploader */ public function setSslVerifyHost($value) { $this->_sslVerifyHost = (int) $value; return $this; } /** * Set CURLOPT_FTP_SSL, default value is CURLFTPSSL_TRY * @example setFtpSsl(CURLFTPSSL_TRY); * @param int $value * @return \Utility\Remote\CurlFtpUploader */ public function setFtpSsl($value) { $this->_ftpSsl = $value; return $this; } /** * Upload the file * [The upload directory has to exist, otherwise upload will fail with an error 9 (CURLE_REMOTE_ACCESS_DENIED (9) * We were denied access to the resource given in the URL. For FTP, this occurs while trying to change to the remote directory)] * @example uploadFile('/var/tmp/pic1.jpg', 'some_dir/dest_pic.jpg'); * @param string $sourceFile * @param string $destFile * @return boolean */ public function uploadFile($sourceFile, $destFile) { if('/' == substr($this->_serverAddress, -1)) { $this->_serverAddress = substr($this->_serverAddress, 0, ( (strlen($this->_serverAddress) - 1 ))); } if('/' == substr($destFile, 0, 1)) { $destFile = substr($destFile, 1); } $uploadDestination = $this->_protocol . '://' . $this->_username . ':' . $this->_password . '@' . $this->_serverAddress . '/' . $destFile; $fp = fopen($sourceFile, 'r'); $ch = curl_init(); curl_setopt($ch, CURLOPT_PORT, $this->_port); curl_setopt($ch, CURLOPT_URL, $uploadDestination); curl_setopt($ch, CURLOPT_UPLOAD, 1); curl_setopt($ch, CURLOPT_INFILE, $fp); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->_sslVerifyPeer); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->_sslVerifyHost); curl_setopt($ch, CURLOPT_FTP_SSL, $this->_ftpSsl); curl_setopt($ch, CURLOPT_INFILESIZE, filesize($sourceFile)); curl_exec ($ch); $this->_errorNo = curl_errno($ch); $this->_error = curl_error($ch); curl_close ($ch); if (0 == $this->_errorNo) { return true; } else { return false; } } }