How to get latest modified csv file values from ftp server and insert it into mysql table

Rahul Narhe

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.

Sorbo

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to save CSV file from MySQL Query in PHP, on server

From Dev

correctly parsing a CSV file from an FTP server with app engine

From Dev

How to insert multiple values in SQL Server table?

From Dev

Get the latest updated file from FTP Folder

From Dev

How to insert values from File_get_contents into Mysql via php Script

From Dev

How to insert values into MySQL database from a dynamic html table?

From Dev

Get the latest file from a remote server from an FTP in Unix

From Dev

Download the latest file from an FTP server

From Dev

How to update SQL server table based on modified values?

From Dev

How to Get VALUES from another table into another table in PHP/MySQL

From Dev

Retrieve modified DateTime of a file from an FTP Server

From Dev

How to use MySql 'Insert Ignore into' query to insert records based on some values from a different table

From Dev

Insert values from csv file to MySQL table

From Dev

How to get FTP server's file separator

From Dev

MYSQL Get id from Table1 and Insert to Table2 while INSERTING Values (USER Form)

From Dev

How to get the latest modified file in a directory with Cakephp

From Dev

Download CSV file from FTP server to save locally and process for Uploading

From Dev

finding the latest modified table in a .txt file

From Dev

Get the latest updated file from FTP Folder

From Dev

unix ftp script to get latest file from server

From Dev

FTP: get the latest file in sever

From Dev

unable to dowload csv file from FTP server in app engine

From Dev

Get the latest file from a remote server from an FTP in Unix

From Dev

Insert only modified values and column names into a table

From Dev

How to use MySql 'Insert Ignore into' query to insert records based on some values from a different table

From Dev

How to get FTP server's file separator

From Dev

How to get insert values from a table into an array using SQL?

From Dev

how to get latest date data from table

From Dev

Unzip csv file from Zip on ftp server

Related Related

  1. 1

    How to save CSV file from MySQL Query in PHP, on server

  2. 2

    correctly parsing a CSV file from an FTP server with app engine

  3. 3

    How to insert multiple values in SQL Server table?

  4. 4

    Get the latest updated file from FTP Folder

  5. 5

    How to insert values from File_get_contents into Mysql via php Script

  6. 6

    How to insert values into MySQL database from a dynamic html table?

  7. 7

    Get the latest file from a remote server from an FTP in Unix

  8. 8

    Download the latest file from an FTP server

  9. 9

    How to update SQL server table based on modified values?

  10. 10

    How to Get VALUES from another table into another table in PHP/MySQL

  11. 11

    Retrieve modified DateTime of a file from an FTP Server

  12. 12

    How to use MySql 'Insert Ignore into' query to insert records based on some values from a different table

  13. 13

    Insert values from csv file to MySQL table

  14. 14

    How to get FTP server's file separator

  15. 15

    MYSQL Get id from Table1 and Insert to Table2 while INSERTING Values (USER Form)

  16. 16

    How to get the latest modified file in a directory with Cakephp

  17. 17

    Download CSV file from FTP server to save locally and process for Uploading

  18. 18

    finding the latest modified table in a .txt file

  19. 19

    Get the latest updated file from FTP Folder

  20. 20

    unix ftp script to get latest file from server

  21. 21

    FTP: get the latest file in sever

  22. 22

    unable to dowload csv file from FTP server in app engine

  23. 23

    Get the latest file from a remote server from an FTP in Unix

  24. 24

    Insert only modified values and column names into a table

  25. 25

    How to use MySql 'Insert Ignore into' query to insert records based on some values from a different table

  26. 26

    How to get FTP server's file separator

  27. 27

    How to get insert values from a table into an array using SQL?

  28. 28

    how to get latest date data from table

  29. 29

    Unzip csv file from Zip on ftp server

HotTag

Archive