3 namespace Utility\Remote;
7 const PROTOCOL_FTP = 'ftp';
8 const PROTOCOL_SFTP = 'sftp';
10 protected $_protocol = self::PROTOCOL_FTP;
11 protected $_port = 21;
13 protected $_serverAddress;
17 protected $_sslVerifyPeer = false;
18 protected $_sslVerifyHost = 0;
20 protected $_ftpSsl = CURLFTPSSL_TRY;
25 function __construct($serverAddress, $username, $password)
27 $this->_serverAddress = $serverAddress;
28 $this->_username = $username;
29 $this->_password = $password;
33 * Set the protocol, default is ftp
34 * @example setProtoCol(CurlFtpUploader::PROTOCOL_SFTP);
35 * @example setProtoCol(CurlFtpUploader::PROTOCOL_FTP);
36 * @param string $protocol
38 * @return \Utility\Remote\CurlFtpUploader
40 public function setProtoCol($protocol)
42 if(self::PROTOCOL_FTP != $protocol && self::PROTOCOL_SFTP != $protocol)
44 throw new \Exception('Protocol must be CurlFtpUploader::PROTOCOL_FTP or CurlFtpUploader::PROTOCOL_SFTP');
46 $this->_protocol = $protocol;
48 if($protocol == self::PROTOCOL_SFTP)
56 * Set the port, default is 21
57 * @example setPort(22);
60 * @return \Utility\Remote\CurlFtpUploader
62 public function setPort($port)
67 throw new \Exception('Port must be an int and bigger than 0');
74 * Set CURLOPT_SSL_VERIFYPEER, default is false
76 * @return \Utility\Remote\CurlFtpUploader
78 public function setSslVerifyPeer($value)
80 $this->_sslVerifyPeer = (bool) $value;
85 * Set CURLOPT_SSL_VERIFYHOST, default is false
87 * @return \Utility\Remote\CurlFtpUploader
89 public function setSslVerifyHost($value)
91 $this->_sslVerifyHost = (int) $value;
95 * Set CURLOPT_FTP_SSL, default value is CURLFTPSSL_TRY
96 * @example setFtpSsl(CURLFTPSSL_TRY);
98 * @return \Utility\Remote\CurlFtpUploader
100 public function setFtpSsl($value)
102 $this->_ftpSsl = $value;
108 * [The upload directory has to exist, otherwise upload will fail with an error 9 (CURLE_REMOTE_ACCESS_DENIED (9)
109 * We were denied access to the resource given in the URL. For FTP, this occurs while trying to change to the remote directory)]
110 * @example uploadFile('/var/tmp/pic1.jpg', 'some_dir/dest_pic.jpg');
111 * @param string $sourceFile
112 * @param string $destFile
115 public function uploadFile($sourceFile, $destFile)
117 if('/' == substr($this->_serverAddress, -1))
119 $this->_serverAddress = substr($this->_serverAddress, 0, ( (strlen($this->_serverAddress) - 1 )));
122 if('/' == substr($destFile, 0, 1))
124 $destFile = substr($destFile, 1);
127 $uploadDestination = $this->_protocol . '://' . $this->_username . ':' . $this->_password . '@' . $this->_serverAddress . '/' . $destFile;
129 $fp = fopen($sourceFile, 'r');
132 curl_setopt($ch, CURLOPT_PORT, $this->_port);
133 curl_setopt($ch, CURLOPT_URL, $uploadDestination);
134 curl_setopt($ch, CURLOPT_UPLOAD, 1);
135 curl_setopt($ch, CURLOPT_INFILE, $fp);
136 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->_sslVerifyPeer);
137 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->_sslVerifyHost);
138 curl_setopt($ch, CURLOPT_FTP_SSL, $this->_ftpSsl);
139 curl_setopt($ch, CURLOPT_INFILESIZE, filesize($sourceFile));
142 $this->_errorNo = curl_errno($ch);
143 $this->_error = curl_error($ch);
146 if (0 == $this->_errorNo)