Page 1 of 1

Need to Copy file to another directory

Posted: Wed Jan 21, 2009 9:05 pm
by wee493
I have a video sharing site with a modification that grabs videos from youtube. Basically long story short i want the videos converted by this converter.php file the be loaded into two directories. /uploads and /uploads/high. I don't know anything about php so could anyone tell me what in need to insert where to copy the converted video to another directory?

Code: Select all

<?php
 
//CHECK POINT 1\\  (uncomment the line below)
//mail("you@yourdomain.com", "PHPmotion - Check Point 1", "Loaded OK", "From: Server <name@email.com>");
 
include_once ("../../classes/config.inc.php");
include_once ("../../classes/config.php");
@error_reporting(0);
 
//LOG THAT THIS PAGE WAS LOADED (debugging for CLI)
if ($log_encoder == "yes") {
    //check if file exists
    $file_contents = "\n\n\n\n".'PHPmotion Convertor.php debug'."\n".
        'CLI for convertor OK'."\n".$config["date_format"]."\n".
        '================================================================================'.
        "\n";
    $log_file = "logs/logfile.rtf";
    if (@file_exists($log_file)) {//append to log file
        $fo = @fopen($log_file,'a');
        @fwrite($fo,$file_contents);
        @fclose($fo);
    }
    else {
        $fo = @fopen($log_file,'w');//else create new log
        @fwrite($fo,$file_contents);
        @fclose($fo);
    }
}
 
/////////////////////////////
//get all file paths for file
/////////////////////////////
$base_path = installation_paths();
$ffmpeg_path = $config["path_to_ffmpeg"];
//////////////////////////////
//PROCESS OR VIDEOS ONE BY ONE
//////////////////////////////
$sql = "SELECT * FROM videos where approved ='pending_youtube'";
$query = @mysql_query($sql);
while ($result = @mysql_fetch_array($query)) {
    $raw_video = $result['video_id'];
    $get_video = $result['location_recorded'];
 
    //set current video to "converting"
    $sql1 = "UPDATE videos SET approved = 'converting' WHERE video_id = '$raw_video'";
    @mysql_query($sql1);
 
    $new_flv = $base_path."/uploads/".$raw_video.".flv";
 
    /////////////////////////////////////////////////////////////
    //                        STEP 1                           //
    //                  download flv from youtube              //
    /////////////////////////////////////////////////////////////
 
    //========================================================================== VITAL CODE =========================================================
 
    //CHECK POINT 2\\ (uncomment the line below)
    //mail("users@gmail.com", "PHPmotion Youtube Grabber- Check Point 2", "Video - Flash : $get_video", "From: Server <name@email.com>");
 
    ///////////////////
    //save file to disk
    ///////////////////
    
    
$file_source = $get_video;
$file_target = $new_flv;
 
  // Preparations
set_time_limit(0);
$file_source = str_replace(' ', '%20', html_entity_decode($file_source)); // fix url format
$rh = fopen($file_source, 'rb');
$wh = fopen($file_target, 'wb');
  while (!feof($rh))
  {
    
    echo '.';// this seems to keep the connection alive
    
fwrite($wh, fread($rh, 1024));
  }
  // Finished without errors
  fclose($rh);
  fclose($wh);
 
    //testing if wget worked, else try wget
    ///////////////////////////////////////
    if (file_exists($new_flv)) {
        //do noting
    }
    else {
        //try wget to download file
        $wget_output = "";
        $wget_command = 'wget -c --output-document='.$new_flv.' "'.$get_video.
            '"';
        @exec("$wget_command 2>&1",$wget_output);
 
        foreach ($wget_output as $outputlines) {
            $wget_debug = $wget_debug.$outputlines."\n";
        }
 
    }
 
    //CHECK POINT 3\\  (uncomment the line below)
    //mail('users@gmail.com','PHPmotion Youtube Grabber- Check Point 3',"Video - Flash : $wget_command  $wget_debug",'From: Server <name@email.com>');
    //========================================================================== VITAL CODE =========================================================
 
    /////////////////////////////////////////////////////////////
    //                        STEP 2                           //
    //                  FLVTOOL2 INJECTION                     //
    /////////////////////////////////////////////////////////////
 
    $path_to_flv = $config["path_to_flvtool2"];
    $flv_cmd = "$path_to_flv -U $new_flv";
    @exec("$flv_cmd 2>&1",$output);//debugging
    $debug_2 = $flv_cmd."\n";//file line of debug
    foreach ($output as $outputline) {
        $debug_2 = $debug_2.$outputline."\n";
        if ($debugmodex == 1) {//no debug mode
            echo ("$outputline<br>");
        }
    }
    //LOG THAT STEP 2 was ok
    if ($log_encoder == "yes") {
        //check if file exists
        $file_contents = 'PHPmotion debug'."\n".$flv_cmd."\n".
            'Command was executed.See rest of log for output details'."\n".
            '================================================================================'.
            "\n";
        $log_file = "logs/logfile.rtf";
        if (@file_exists($log_file)) {//append to log file
            $fo = @fopen($log_file,'a');
            @fwrite($fo,$file_contents);
            @fclose($fo);
        }
        else {
            $fo = @fopen($log_file,'w');//else create new log
            @fwrite($fo,$file_contents);
            @fclose($fo);
        }
    }
    
 
    /////////////////////////////////////////////////////////////
    //                        STEP 3                           //
    //                  ffmpeg-php get video duration          //
    /////////////////////////////////////////////////////////////
 
    //Original FFMPEG-PHP option for getting video length
    ///////////////////////////////////////////////////////////////////
 
    /*
    $video_info = @new ffmpeg_movie($new_flv);//duration of new flv file.
 
    $sec = @$video_info->getDuration();// Gets the duration in secs.
    */
    //-------------------------------------------------------------------------------end of original ffmpeg-php
    //Alternative to flvtool2 if getting errors or if no flvtool exists (comment out if failing or to use ffpmeg-php)
    ///////////////////////////////////////////////////////////////////
 
    //Try and read the time from the output files
    $shell_output = $debug_1 . $debug_2;//get as much sheel out put as possible to run search for duration
    if (@preg_match('/Video stream:.*bytes..(.*?).sec/', $shell_output, $regs)) {
        $sec = $regs[1];
        $duration = sec2hms($sec);//covert to 00:00:00 i.e. hrs:min:sec
    }
    else {
        $sec = "";
    }
 
    //check if duration has been picked up...if not try second method  (ffmpeg -i video.flv)
    if ($sec == "" || !is_numeric($sec)) {
        $check_duration = $ffmpeg_path . ' -i ' . $new_flv;
        $durationfile = "";
        @exec("$check_duration 2>&1", $durationoutput);
 
        foreach ($durationoutput as $outputline) {
            $durationfile = $durationfile . $outputline . "\n";
 
        }
 
        if (preg_match('/uration:.(.*?)\./', $durationfile, $regs)) {
            $duration = $regs[1];//duration already in 00:00:00 format
            $sec = date("s", strtotime($duration));//change back to seconds for use in getting middle of video
        }
        else {
            $sec = 2;
            $duration = sec2hms($sec);//covert to 00:00:00 i.e. hrs:min:sec
        }
 
    }
    //get the middle of the movie (time; 00:00:00 format) for thumbnail
    $sec2 = $sec / 2;
    $sec2 = @round($sec2);
    $thumb_position = sec2hms($sec2);
    //LOG THAT STEP 3 was ok
    if ($log_encoder == "yes") {
        //check if file exists
        $file_contents = 'PHPmotion debug'."\n".
            'FFMPEG-PHP - check - Video Duration = '.$duration."\n".
            '================================================================================'.
            "\n";
        $log_file = "logs/logfile.rtf";
        if (@file_exists($log_file)) {//append to log file
            $fo = @fopen($log_file,'a');
            @fwrite($fo,$file_contents);
            @fclose($fo);
        }
        else {
            $fo = @fopen($log_file,'w');//else create new log
            @fwrite($fo,$file_contents);
            @fclose($fo);
        }
    }
 
    //CHECK POINT 4\\ (uncomment the line below)
    //mail("you@yourdomain.com", "PHPmotion - Check Point 4", "FFMPEG-PHP : $duration", "From: Server <name@email.com>");
 
    /////////////////////////////////////////////////////////////
    //                        STEP 4                           //
    //                  Create thumnail image                  //
    /////////////////////////////////////////////////////////////
 
    $output_file = $base_path.'/uploads/thumbs/'.$raw_video.'.jpg';
    //IMPORTANT: if your thumbs ar enot being created, try change "mjpeg" below to "image2"
    $ffmpeg_cmd2 = "$config[path_to_ffmpeg] -i $new_flv -ss $thumb_position -t 00:00:01 -s 120x90 -r 1 -f mjpeg $output_file";
    //execute and record output to variable
    @exec("$ffmpeg_cmd2 2>&1",$output);//debugging
    $debug_3 = $ffmpeg_cmd2."\n";//file line of debug
    foreach ($output as $outputline) {
        $debug_3 = $debug_3.$outputline."\n";
        if ($debugmodex == 1) {//no debug mode
            echo ("$outputline<br>");
        }
    }
    //LOG THAT STEP 4 was ok
    if ($log_encoder == "yes") {
        //check if file exists
        $file_contents = 'PHPmotion debug'."\n".$ffmpeg_cmd2."\n".
            'Command was executed.See rest of log for output details'."\n".
            '================================================================================'.
            "\n\n\n";
        $log_file = "logs/logfile.rtf";
        if (@file_exists($log_file)) {//append to log file
            $fo = @fopen($log_file,'a');
            @fwrite($fo,$file_contents);
            @fclose($fo);
        }
        else {
            $fo = @fopen($log_file,'w');//else create new log
            @fwrite($fo,$file_contents);
            @fclose($fo);
        }
    }
 
    //////////////////////////////////////////////////////////////
    //check if image thumb has been made
    //This tries to create thumb using different format "image2"
    //////////////////////////////////////////////////////////////
 
    if (@!file_exists($output_file)) {
        $ffmpeg_cmd2 = "$config[path_to_ffmpeg] -i $new_flv -ss $thumb_position -t 00:00:01 -s 120x90 -r 1 -f image2 $output_file";
        //execute and record output to variable
        @exec("$ffmpeg_cmd2 2>&1",$output);//debugging
        $debug_3 = $ffmpeg_cmd2."\n";//file line of debug
        foreach ($output as $outputline) {
            $debug_3 = $debug_3.$outputline."\n";
            if ($debugmodex == 1) {//no debug mode
                echo ("$outputline<br>");
            }
        }
        //LOG THAT STEP 4 was ok
        if ($log_encoder == "yes") {
            //check if file exists
            $file_contents = 'PHPmotion debug - Image type 2'."\n".$ffmpeg_cmd2.
                "\n".'Command was executed.See rest of log for output details'.
                "\n".
                '================================================================================'.
                "\n\n\n";
            $log_file = "logs/logfile.rtf";
            if (@file_exists($log_file)) {//append to log file
                $fo = @fopen($log_file,'a');
                @fwrite($fo,$file_contents);
                @fclose($fo);
            }
            else {
                $fo = @fopen($log_file,'w');//else create new log
                @fwrite($fo,$file_contents);
                @fclose($fo);
            }
        }
    }
 
    //CHECK POINT 5\\  (uncomment the line below)
    //mail("you@yourdomain.com", "PHPmotion - Check Point 5", "THUMBNAIL : $ffmpeg_cmd2", "From: Server <name@email.com>");
 
    /////////////////////////////////////////////////////////////
    //                        STEP 6                           //
    //                  UDATE DATABASE DETAILS                 //
    /////////////////////////////////////////////////////////////
 
 
 
 
 
    //--------------new code 28 october 2007 --------reconnect mysql ---------
    $sql_test = 'SELECT * FROM admin';
    if (!mysql_query($sql_test)) {
        @mysql_close();
        @include_once ("../../classes/config.inc.php");
        @include_once ("../../classes/config.php");
        @mysql_connect($hostname,$dbusername,$dbpassword,true);
        @mysql_select_db($dbname);
    }
    //--------------new code 28 october 2007 --------reconnect mysql end -----
    
    
    
    
 
    //RESET THE VIDEO FROM "pending_conversion" to just "pending" (for admin approval etc)
    $sql = "UPDATE videos SET approved='pending' WHERE video_id = '$raw_video'";
    @mysql_query($sql);//UPDATE LOCATION TO NONE
    $sql = "UPDATE videos SET location_recorded='none' WHERE video_id = '$raw_video'";
    @mysql_query($sql);//Update dabase with new duration information
    /////////////////////////////////////////////
    $file_name_no_extension = $file_name_no_extension;
    $sql = "UPDATE videos SET video_length='$duration' WHERE video_id = '$raw_video'";
    @mysql_query($sql);//Check if video need pre approval by admin, if not update from pending to "yes"
    ////////////////////////////////////////////////////////////////////////////////
    if ($config["auto_approve_videos"] == "yes") {
        $sql = "UPDATE videos SET approved='yes' WHERE video_id = '$raw_video'";
        @mysql_query($sql);
    }
    //@mysql_close();
    //////////////////////////////////////////////////////////////////
    //WRITE OUTPUT TO LOGFILE - HELP WITH DEBUGGING (logs/logfile.rtf)
    //////////////////////////////////////////////////////////////////
    if ($log_encoder == "yes") {
        $file_contents = 'Date: '.$config["date_format"]."\n".'STEP 1 - OUTPUT'.
            "\n".$debug_1."\n".'STEP 2 - OUTPUT'."\n".$debug_2."\n".
            'STEP 4 - OUTPUT'."\n".$debug_3;
        //adding all output
        //check if file exists
 
        $log_file = "logs/logfile.rtf";
        if (@file_exists($log_file)) {//append to log file
            $fo = @fopen($log_file,'a');
            @fwrite($fo,$file_contents);
            @fclose($fo);
        }
        else {
            $fo = @fopen($log_file,'w');//else create new log
            @fwrite($fo,$file_contents);
            @fclose($fo);
        }
    }
    //delete original file and converted temp avi file
    ///////////////////////////////////////////////////
    $original_file = $raw_video_path;
    if ($config["delete_original"] == 'yes') {
        if (@file_exists("$new_flv") && @file_exists("$raw_video_path")) {
 
            if ($new_flv != $raw_video_path) {
                @unlink($raw_video_path);
            }
        }
    }
 
    if ($config["delete_avi"] == 'yes') {
        if (@file_exists("$new_flv") && @file_exists("$avi_file")) {
            @unlink($avi_file);
        }
    }
 
    //CHECK POINT 6\\  (uncomment the line below)
    //mail("you@yourdomain.com", "PHPmotion - Check Point 6", "SCRIPT LOADED TO THE END", "From: Server <name@email.com>");
 
}
@mysql_close();//end while
?>

Re: Need to Copy file to another directory

Posted: Wed Jan 21, 2009 9:09 pm
by Burrito
look at copy()

Re: Need to Copy file to another directory

Posted: Wed Jan 21, 2009 9:12 pm
by wee493
Burrito wrote:look at copy()
I saw that when i did some google searching, but I just don't know where to insert it into the script.

Re: Need to Copy file to another directory

Posted: Wed Jan 21, 2009 9:14 pm
by Burrito
I'm not going to scour your code...but looks like (at a quick glance) line 86ish area.

Re: Need to Copy file to another directory

Posted: Wed Jan 21, 2009 9:38 pm
by wee493
Burrito wrote:I'm not going to scour your code...but looks like (at a quick glance) line 86ish area.
I added this code, but now the script does not work at all
$file2 = '/high'$new_flv;

if (!copy($new_flv, $file2)) {
echo "failed to copy $file...\n";
}

Re: Need to Copy file to another directory

Posted: Wed Jan 21, 2009 9:40 pm
by Burrito
change
$file2 = '/high'$new_flv;
to
$file2 = '/high/'.$new_flv;

Re: Need to Copy file to another directory

Posted: Wed Jan 21, 2009 9:48 pm
by wee493
Burrito wrote:change
$file2 = '/high'$new_flv;
to
$file2 = '/high/'.$new_flv;
Thanks for point that out, I guess I missed that. But the script its still now working.