PHP CLI FTP uploader
February 21st, 2009
No comments
So I wanted to create a automated FTP uploader for the project I was working on. I set a CRONTAB to execute it and then hooked up a database as well.
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | #!/usr/bin/php -q <?php #script used for uploading file to a server //------ Begin Programming $ftp_server = "subdomain.server.com"; $ftp_retries = "5"; $ftp_delay = "3"; $ftp_user = "user"; $ftp_pass = "password"; //This worked on the server $source_directory = "/home/user/videos/flv/"; $remote_directory = "/httpdocs/movies/"; //mysql connection define ('DB_User', 'user'); define ('DB_Password', 'password'); define ('DB_Host', 'x.x.x.x'); define ('DB_Name', 'db_name'); $dbc = mysql_connect (DB_Host, DB_User, DB_Password) OR die ('Could not connect to MySQL Server: ' . mysql_error() ); mysql_select_db (DB_Name) or die ('Could not select the database: ' . mysql_error() ); //do a query $sql_query = "your query"; $result = @mysql_query ($sql_query); $videos = mysql_fetch_array ($result, MYSQL_ASSOC); $counter = mysql_num_rows($result); if ($counter > 0) { //mark the as processing $sql_query = "UPDATE SCRIPT"; $result = @mysql_query ($sql_query); // $conn_id = ftp_connect($ftp_server); $login_result = ftp_login($conn_id, $ftp_user, $ftp_pass); $source = $source_directory.$videos['processed_file'].".flv"; $remote = $remote_directory.$videos['processed_file'].".flv"; if (ftp_put($conn_id, $remote, $source, FTP_BINARY)) { // echo "successfully uploaded $source\n"; } else { //mark file to reupload again $sql_query = "UPDATE SCRIPT"; $result = @mysql_query ($sql_query); // echo "There was a problem while uploading $source to $remote\n"; } // Close connection ftp_close($conn_id); } ?> |