PHP Driven Recording Archive Script

Discussions about development of VICIDIAL and astGUIclient

Moderators: gerski, enjay, williamconley, Op3r, Staydog, gardo, mflorell, MJCoate, mcargile, Kumba, Michael_N

PHP Driven Recording Archive Script

Postby Acidshock » Sat Feb 25, 2012 11:53 am

I know an archive script already exists but I decided to write one in php yesterday that simply plugs into my normal "cleanout" operation on my recordings. Maybe not as robust as the other one but I figured people may have a need for it regardless. I use this to upload to a NAS that a customer has implemented. Benefits of this script are that its extremely simple, organizes the files by Year/Month/Day, and is very flexible. It can be used via commandline, other scripts, called from another web based script, cron, etc.



Code: Select all
#!/usr/bin/php -q
<?php
//Written by Drew Gilkey. 2/24/2012
//Syntax:
//archive.php file.wav
//Script Parameters(Begin)
set_time_limit(0);
date_default_timezone_set('America/Los_Angeles');


$FTP_SERVER = "myarchiveserver.com"; //FTP Server Host Address
$FTP_BASELOC = "/public_html/"; //Folder to upload to
$FTP_USERNAME = "recordings";//FTP Username
$FTP_PASSWORD = "1234";//FTP Password
$DB_USER = "cron";//VICIDIAL DB Username
$DB_PASSWORD = "1234";//VICIDIAL DB Password(Hope you arent using the default)
$DB_SERVER = "localhost"; //DB Server address. Localhost for single server.
$WEB_SERVER = "http://www.myrecordings.com/";//Web Server address
$WEB_PATH = "~recordings";//Path to access the recordings(the ftp_baseloc)
//Parameters End

if(file_exists($argv[1]))
{
//Grab info on the file.
   $year =  date("Y", filemtime($argv[1]));
   $day =  date("d", filemtime($argv[1]));
   $month =  date("F", filemtime($argv[1]));
    $info = pathinfo($argv[1]);
 
//Connect to the FTP server
   $conn_id = ftp_connect($FTP_SERVER) or die("Couldnt Connect to FTP");
   if(ftp_login($conn_id, $FTP_USERNAME, $FTP_PASSWORD))
   {
//Create the folders. If already created a php warning will happen.
        try
        {
          ftp_mkdir($conn_id, "$FTP_BASELOC/$year");
        }
        catch(Exception $e)
        {
            echo "\nYear already created.\n";
        }
        try
        {
          ftp_mkdir($conn_id, "$FTP_BASELOC/$year/$month");
        }
        catch(Exception $e)
        {
            echo "\nMonth already created.\n";
        }
        try
        {
      ftp_mkdir($conn_id, "$FTP_BASELOC/$year/$month/$day");
        }
        catch(Exception $e)
        {
            echo "\nDay already created.";
        }

//Upload the file
       
      if(ftp_put($conn_id, "$FTP_BASELOC/$year/$month/$day/" . basename($argv[1]), $argv[1], FTP_BINARY))
      {
         echo "Successful transmission";
//Connect to DB
            mysql_connect($DB_SERVER,$DB_USER,$DB_PASSWORD);   
            @mysql_select_db("asterisk") or die( "Unable to select database");
//UPDATE location of the recording
            $query = "UPDATE recording_log SET location='" . $WEB_SERVER . $WEB_PATH . "/" . $year . "/" . $month . "/" . $day . "/" . basename($argv[1]) .  "' WHERE filename='" . basename($argv[1], "-all." . $info['extension']) . "';";
            //echo $query;
            $result = mysql_query($query); //Uncomment to see query being sent to DB server.
            if($result)
            {
//IF DB change successful and file was uploaded delete original on local server.
                echo "\nFile changed in DB Successful\n";
                unlink($argv[1]);
            }
            else
            {
//Error happened most likely something DB related.
                echo "\nCannot rename file in the database.... something went wrong!\n";
            }
            //$rowcount = mysql_numrows($result);
            mysql_close();
      }
   }

}
else
{
//Probably wasnt given the proper file or location.
    echo "\nERROR FILE DOES NOT EXIST!\n";
}
?>


How its executed:
Code: Select all
/usr/bin/archive.php blah.mp3


I put this in the crontab
Code: Select all
24 0 * * * /usr/bin/find /var/spool/asterisk/monitorDONE/MP3 -maxdepth 2 -type f -mtime +7 -print | xargs -L 1 /usr/bin/archive.php


I will be putting this in the Issue Tracker as well.
Acidshock
 
Posts: 430
Joined: Wed Mar 03, 2010 3:19 pm

Re: PHP Driven Recording Archive Script

Postby williamconley » Sun Apr 29, 2012 12:31 pm

Excellent share 8-)
Vicidial Installation and Repair, plus Hosting and Colocation
Newest Product: Vicidial Agent Only Beep - Beta
http://www.PoundTeam.com # 352-269-0000 # +44(203) 769-2294
williamconley
 
Posts: 20253
Joined: Wed Oct 31, 2007 4:17 pm
Location: Davenport, FL (By Disney!)


Return to Development

Who is online

Users browsing this forum: No registered users and 19 guests