0

My setup: Server 2008 R2 IIS 7.5 PHP 5

I'm looking to have a PHP script on one of the pages that executes a command line exe with parameters that the webpage can then use the output of.

I have a command line program that converts a video and saves it to the location from which the command was run, I want php to do that from the webpage and then have the web page be able to take the converted file and put it in an embedded player for watching??

Is this even possible :-s

j08691
  • 204,283
  • 31
  • 260
  • 272
WindowsDan
  • 59
  • 1
  • 9
  • 2
    **What have you tried so far?** – Markus Hofmann Oct 09 '13 at 19:04
  • https://www.google.com/search?q=I'm+looking+to+have+a+PHP+script+on+one+of+the+pages+that+executes+a+command+line+exe+with+parameters+that+the+webpage+can+then+use+the+output+of.&oq=I'm+looking+to+have+a+PHP+script+on+one+of+the+pages+that+executes+a+command+line+exe+with+parameters+that+the+webpage+can+then+use+the+output+of.&aqs=chrome..69i57.1766j0j8&sourceid=chrome&espvd=210&es_sm=93&ie=UTF-8 – Sebas Oct 09 '13 at 19:05
  • possible duplicate of [PHP retrieve external program data](http://stackoverflow.com/questions/2778122/php-retrieve-external-program-data) – aynber Oct 09 '13 at 19:05
  • putting your question title in google hit 1 = http://php.net/manual/en/function.exec.php (ok, after your question) –  Oct 09 '13 at 19:08

3 Answers3

0

The function you are looking for is exec().

Make sure you escape parameters before using them in the command: escapeshellarg()

$param = $_POST["param"];
// some sort of sanitizing happens here
...

then your command

$output  = exec("videoconverter ".escapeshellarg($param));
Ibu
  • 42,752
  • 13
  • 76
  • 103
  • Watch out using `exec` with php. It can become harmful if used incorrectly. Also shared hosts (in case you're hosting your project on one) may completely disable exec, system, and shell commands in php to prevent resource hogging and abuse. – Markus Hofmann Oct 09 '13 at 19:09
  • True, even a condom may become harmful. – Markus Hofmann Oct 09 '13 at 20:10
0

It is possible with PHP. However, there are more suited programming languages like Phyton, Ruby, C++ or C# which cope with background tasks better.

To carry out background tasks in PHP you'd need to use something like exec, system, shell_exec, popen or something more advanced like fsockopen and pcntl_fork.

It all depends on your stack. There are job servers like Gearman which need some setup, but make working with jobs in php easier as the processing is handled by such servers.

An approach to what you want to achieve:

  1. Create a php script that converts and saves the video

  2. CHMOD the file with 755 to make it executable in CLI

  3. Run the script in the background as soon as a video should be converted, like so:

pclose(popen('start "Process" /B "C:\PHP\php-cgi.exe" -f video_converter.php 2>&1 &', 'r'));

"Process" is the title of the background cli prompt. It's required to prevent problems. /B starts the command without a prompt window. The output of the script will be supressed with 2>&1 & to prevent interruptions.

However, I'd let the script write (e.g. file_put_contents) the convertor-progress to a file and listen with ajax (javascript / jquery) on the website the player resides on. As soon as 100% is reached, the video filepath is returned and the javascript updates and starts the embedded player.

For large video files that would make the convertion take too long to wait, you could send the user an email as soon as the video is ready to be watched - in case you make this application available to the public.

NOTE: The command is specific for windows servers. Non-Windows OS like *NIX, Linux and Sun the following command is used:

shell_exec(sprintf('nohup /usr/bin/env php -q -f %s > /dev/null 2>&1 & echo $!', 'path/to/video_converter.php'));

nohup is no hangup and keeps the background process running until it's terminated. The rest is about the same as the windows command, just modified a little.

Give it a try and let us know if it worked :-)

Here are some resources worth reading with more info on this topic:

Community
  • 1
  • 1
Markus Hofmann
  • 3,427
  • 4
  • 21
  • 31
0

CRACKED IT!!

Thanks for the help but managed to get it to work :)

I used a command line tool that I passed an command within back ticks to in. the php webpage. for the program to download the video from a given url (which is the variable pulled from the POST of a form) and the put that downloaded video back into an embedded HTLM 5 video element that has a fallback to flash.

I created if statements to pull mp4 for certain browsers and webm for others so it should always work, with the flash for older browsers not supporting the tag.

Works nicely :)

Code for reference:-

<?php
$input = $_POST['url'];
function browser_info($agent=null) {
  $known = array('msie', 'firefox', 'safari', 'webkit', 'opera', 'netscape',
    'konqueror', 'gecko');
  $agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
  $pattern = '#(?<browser>' . join('|', $known) .
    ')[/ ]+(?<version>[0-9]+(?:\.[0-9]+)?)#';
  if (!preg_match_all($pattern, $agent, $matches)) return array();
  $i = count($matches['browser'])-1;
  return array($matches['browser'][$i] => $matches['version'][$i]);
};
$ua = browser_info();

if($ua['firefox']):
$filename = `C:\Windows\dl.exe -e -f 45/44/43/35/34/5 $input`;
$download = `C:\Windows\dl.exe -o "%(title)s.%(ext)s" -f 45/44/43/35/34/5 $input`;
$output = `C:\Windows\dl.exe -g -f 45/44/43/35/34/5 $input`;

else:
$filename = `C:\Windows\dl.exe -e -f 22/18/35/34/5 $input`;
$download = `C:\Windows\dl.exe -o "%(title)s.%(ext)s" -f 22/18/35/34/5 $input`;
$output = `C:\Windows\dl.exe -g -f 22/18/35/34/5 $input`;

endif;
?>
<html>
<head>
<script type="text/javascript">
window.onunload=function(){
window.open('/watch/close.php', '_blank', '');
window.close();
};
</script>
<link rel="stylesheet" href="../css/navigation.css" type="text/css" media="screen" />
<link rel="stylesheet" href="index.css" type="text/css" media="screen" />
</head>
<body>
<ul class="t1">
<li class="t2"><a href="/">Home</a></li>
<li class="t2 t3"><a href="../Video">Videos</a></li>
<li class="t2 t4"><a href="/Downloads">Downloads</a></li>
<li class="t2 t8"><a href="/web">Proxy</a></li>
<li class="t2 t5"><a href="../Forsale">For Sale</a></li>
<li class="t2 t6"><a href="/owa">E-mail</a></li>
<li class="t2 t7"><a href="/CV">My CV</a></li>
</ul>
<br />
<div class="white">
<video controls id="vid" width="800" height="450" autoplay="autoplay" preload="none" poster="../Images/loading.jpg">
<source src="<?php echo $filename; ?>.mp4">
<source src="<?php echo $filename; ?>.webm" type="video/webm; codecs=&quot;vp8, vorbis&quot;">
<object>
<embed
src="player.swf" 
width="640" height="360" 
bgcolor="000000" 
allowscriptaccess="always" 
allowfullscreen="true" 
type="application/x-shockwave-flash" 
flashvars="width=640&height=360&type=video&fullscreen=true&volume=100&autostart=true&file='<?php echo $filename; ?>.flv'" 
/>
</object>
</video>
</div>
<div class="white">
<a onmouseover="self.status='Downloadable file'; return true" onmouseout="self.status=''" onclick="alert('Please right click and use \'Save Target/Link As\'.'); return false"  href="<?php echo $output; ?>">Download this video</a>
</div>
</body>
</html>
WindowsDan
  • 59
  • 1
  • 9