curlHandle = curl_init(); //init class vars if necessary $this->ftpHost = $host; $this->ftpPort = $port; $this->ftpUserName = $user; $this->ftpPass = $pass; $this->ftpRemoteDir = $remoteDir; $this->ftpURL = $url; } public function __destruct() { curl_close($this->curlHandle); } //add files to be transferred to FTP //param: $fileName - array containing original name, remote name, submission_type public function addFiles($fileName) { if( file_exists($fileName['local_file']) ) { $this->filesToTransfer[] = $fileName; return true; }else{ return false; } } //set connection options //param: $key - name of the var //param: $value - value to be set for var public function setOptions($key, $value) { $this->$key = $value; } //set the options for cURL public function setCurlOptions($key, $value) { curl_setopt($this->curlHandle, $key, $value); } //retrieve the cURL transfer details //param: $option - retrieve specific detail public function getTransferDetails($option = '') { return $option != '' ? curl_getinfo($this->curlHandle, $option) : curl_getinfo($this->curlHandle); } //returns the last error for current cURL session public function getLastError() { return curl_error($this->curlHandle); } //upload the files to the FTP server //return: the cURL error code if an error occured, 0 (zero) if succeded public function submitFiles() { $error = ''; //in case the cURL session was closed and not re-opened, open a new connection if( !$this->curlHandle ) { $this->curlHandle = curl_init(); } $tmpURL = "ftp://". $this->ftpUserName . ":" . $this->ftpPass . "@" . $this->ftpHost . "/"; if( strlen($this->ftpRemoteDir) ) $tmpURL .= $this->ftpRemoteDir . '/'; foreach($this->filesToTransfer as $key=>$value) { $fp = fopen($this->filesToTransfer[$key]['local_file'], 'rb'); $this->setCurlOptions(CURLOPT_URL, $tmpURL . $this->filesToTransfer[$key]['remote_file_name']); $this->setCurlOptions(CURLOPT_UPLOAD, '1'); $this->setCurlOptions(CURLOPT_INFILE, $fp); $this->setCurlOptions(CURLOPT_PORT, $this->ftpPort); $this->setCurlOptions(CURLOPT_INFILESIZE, filesize($this->filesToTransfer[$key]['local_file'])); curl_exec($this->curlHandle); fclose($fp); $error = curl_errno($this->curlHandle); } return $error; } }