I am trying to get latest modified csv file values from ftp server and insert it into mysql table. I don't have any idea how to do this.
I have to just read csv file from ftp server and get the latest data and insert into my database. I am able to do this by downloading the latest file to my system. So i just have to do it without downloading.
Example:
function getFtpConnection()
{
static $connection = null;
if ($connection !== null) return $connection;
$host = '10.0.0.1';
$username = 'username';
$password = 'password';
// set up connection
if (!$connection = ftp_connect($host)) {
echo "couldn't connect to " . $host;
return false;
}
// login with username and password
if (!$login_result = ftp_login($connection, $username, $password)) {
echo "couldn't connect as " . $username;
return false;
}
return $connection;
}
function getCsvFiles()
{
if (!$connection = getFtpConnection()) return false;
$folder = '/folder/';
// get list of files on given path
$files = ftp_nlist($connection, $folder);
if (!count($files)) {
echo "folder is empty";
return false;
}
$csvFiles = array();
foreach ($files as $file) {
if (!preg_match('~\w+.csv$~ism', $file)) continue;
$csvFiles[] = $folder . '/' . $file;
}
return $csvFiles;
}
function findNewestFile($files)
{
$mostRecent = array(
'time' => 0,
'file' => null
);
foreach ($files as $file) {
// get the last modified time for the file
$time = ftp_mdtm(getFtpConnection(), $file);
if ($time > $mostRecent['time']) {
// this file is the most recent so far
$mostRecent['time'] = $time;
$mostRecent['file'] = $file;
}
}
return $mostRecent['file'];
}
// configuration
$dbtype = "mysql";
$dbhost = "localhost";
$dbname = "test";
$dbuser = "root";
$dbpass = "admin";
// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
// All FTP files
$files = getCsvFiles();
// find newest file
$newestFile = findNewestFile($files);
// query
$sql = "INSERT INTO csv_files (newestFile) VALUES (:newestFile)";
$q = $conn->prepare($sql);
$q->execute(array(':newestFile' => $newestFile));
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments