public function downloadFromFulfillment() { $arrayCsvFiles = array(); $arrayCsvFilesClear = array(); $sql = "SELECT *\n FROM `fulfillment_files`\n WHERE `fulfillment_id`=?i"; $result = self::$_msql->getInd('filename', $sql, $this->fulfillment_id); if ($this->ssl) { $sftp = new Net_SFTP($this->server); if (!$sftp->login($this->username, $this->password)) { exit('Login Failed'); } //$contents = $sftp->nlist("{$this->download_path}"); $dPath = trim($this->download_path, '/'); $contents = $sftp->nlist("{$dPath}"); if (!$contents) { return; } foreach ($contents as $file) { if (substr($file, -4, 4) == '.csv') { $arrayCsvFiles[] = $arrayCsvFilesClear[] = $file; } } foreach ($arrayCsvFiles as $file) { $tF = explode('/', $file); $fName = array_pop($tF); if (isset($result["{$fName}"])) { continue; } $downloadFilePath = AF::path(time() . rand(1, 1000), array('files', 'csv', 'temp'), 'csv'); if ($sftp->get($dPath . '/' . $file, $downloadFilePath)) { self::parserCsvFiles($downloadFilePath); $sql = "INSERT INTO `fulfillment_files` (`fulfillment_id`, `filename`, `time`) VALUES (?i, ?s, NOW())"; self::$_msql->query($sql, $this->fulfillment_id, $fName); } } $sftp->_disconnect(0); } else { $ftp = ftp_connect($this->server, $this->port, 300); if (!$ftp) { return; } if ($this->username && $this->password) { ftp_login($ftp, $this->username, $this->password); } ftp_pasv($ftp, true); // Passive mode $contents = ftp_nlist($ftp, "{$this->download_path}"); if (!$contents) { return; } foreach ($contents as $file) { if (substr($file, -4, 4) == '.csv') { $file = str_replace('\\', '/', $file); $arrayCsvFiles[] = $file; $tF = explode('/', $file); $arrayCsvFilesClear[] = (string) trim(array_pop($tF)); } } foreach ($arrayCsvFiles as $file) { $tF = explode('/', $file); $fName = (string) trim(array_pop($tF)); if (isset($result["{$fName}"])) { continue; } $downloadFilePath = AF::path(time() . rand(1, 1000), array('files', 'csv', 'temp'), 'csv'); if (ftp_get($ftp, $downloadFilePath, $file, FTP_ASCII)) { self::parserCsvFiles($downloadFilePath); $sql = "INSERT INTO `fulfillment_files` (`fulfillment_id`, `filename`, `time`) VALUES (?i, ?s, NOW())"; self::$_msql->query($sql, $this->fulfillment_id, $fName); } } ftp_close($ftp); } $sql = "DELETE FROM `fulfillment_files`\n WHERE `fulfillment_id` = ?i\n AND `filename` NOT IN (?a)"; self::$_msql->query($sql, $this->fulfillment_id, $arrayCsvFilesClear); }
$arrayCsvFiles[] = $arrayCsvFilesClear[] = $file; } } foreach ($arrayCsvFiles as $file) { if (isset($result["{$file}"])) { continue; } $localFileNeme = time() . rand(1, 1000); $downloadFilePath = AF::path($localFileNeme, array('files', 'csv', 'rockets'), 'csv'); if ($sftp->get($dPath . '/' . $file, $downloadFilePath)) { parse_csv_file($downloadFilePath); $sql = "INSERT INTO `fulfillment_files` (`fulfillment_id`, `filename`, `time`) VALUES (?i, ?s, NOW())"; $msql->query($sql, $fulfillmentID, $file); } } $sftp->_disconnect(0); function parse_csv_file($downloadFilePath) { global $fulfillmentID, $msql; $header = NULL; $data = array(); if (($handle = fopen($downloadFilePath, 'r')) !== FALSE) { while (($row = fgetcsv($handle, 1000, ',')) !== FALSE) { if (!$header) { $header = $row; } else { $data[] = array_combine($header, $row); } } fclose($handle); }