user2475259
user2475259

Reputation: 31

How to upload file to FTP using cron jobs

I want to be able to upload a csv file once a day (locally from my pc) to ftp. I then am going to insert this csv file into a mysql table.

I've created the cron job to pick up a csv and insert it into the database, but I'm struggling how to figure out how to pick up a file that is on my loacl pc and upload it to FTP.

Has anyone got any ideas?

Thanks Adi

Upvotes: 3

Views: 2356

Answers (2)

Dinesh Chandra
Dinesh Chandra

Reputation: 11

// open some file for reading

$file = 'somefile.txt';

$fp = fopen($file, 'r');

$ftp_user_name="xxxxxx";

$ftp_user_pass="xxxxx";

// set up basic connection

//$conn_id = ftp_connect($ftp_server);

$conn_id = ftp_connect("xxxxxxx.com", 21) or die("failed to connect");

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to upload $file

if (ftp_fput($conn_id, $file, $fp, FTP_ASCII)) {

    echo "Successfully uploaded $file\n";
} else {

    echo "There was a problem while uploading $file\n";
}

// close the connection and the file handler

ftp_close($conn_id);

fclose($fp);

Upvotes: 1

Juan Basso
Juan Basso

Reputation: 43

You can do using the ftp extension in PHP, something like:

$conn = ftp_connect("destination.host", 21) or die("failed to connect");
ftp_login($conn, $user, $pass) or die("failed to login");
ftp_put($conn, "/path/on/ftp/server", "/path/on/your/local", FTP_BINARY) or die("failed to upload);

More details: https://www.php.net/manual/en/book.ftp.php

Upvotes: 2

Related Questions