Saturday, February 21, 2009

PHP CLI FTP Uploader

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:
#!/usr/bin/php -q
#script used for uploading file to a server
//------ Begin Programming

$ftp_server = "";
$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 &gt; 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

No comments:

Post a Comment

Raspberry Pi Zero W - Wireless Configuation

create the file under "boot" folder wpa_supplicant.conf country=GB ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev u...