passing variable to exec function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
quky
Forum Newbie
Posts: 1
Joined: Fri Jul 15, 2011 6:32 pm

passing variable to exec function

Post by quky »

Ok I am new to php so bear with me
I am trying to pass some variables to an exec function like

Code: Select all

<?php 
$val0 = $_GET['tstart']; /// int value 
$val1 = $_GET['tduration']; /// int value 
$val2 = $_GET['Rname'];  /// string value 
//// cropfile.php?tstart=30&tduration=40&Rname=foo.mp3 
echo "the word is:  $val0  and $val1"; 
exec('ffmpeg -ss intval($val0) -t intval($val1) -i $val2 -acodec copy newfoo.mp3 2>&1 &', $out, $rv);
echo "output is:<br>\n".implode("<br>\n", $out)."<br>\nexit code:$rv<br>\n"; 
?>
And for some reason is not taken the values
the syntax
is exec('ffmpeg -ss 00:00:30.00 -t 25 -i foo.mp3 -acodec copy -y bar-new.mp3 2>&1 &', $out, $rv);
What I am missing here
Best Regards
HP :mrgreen:
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: passing variable to exec function

Post by twinedev »

You cannot use variables directly inside of single quotes, you need to either use double quotes, or (for better readability IMO), use them outside of the quotes.

Code: Select all

exec("ffmpeg -ss intval($val0) -t intval($val1) -i $val2 -acodec copy newfoo.mp3 2>&1 &", $out, $rv);
or

Code: Select all

exec('ffmpeg -ss intval('.$val0.') -t intval('.$val1.') -i '.$val2.' -acodec copy newfoo.mp3 2>&1 &', $out, $rv);
See http://php.net/manual/en/language.types.string.php for more info on the difference between singe and double quotes.

-Greg
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: passing variable to exec function

Post by AbraCadaver »

You never want to trust user submitted data and don't want to pass it to exec() un-escaped: http://us2.php.net/manual/en/function.e ... ellarg.php
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
beetree
Forum Commoner
Posts: 26
Joined: Mon Jul 18, 2011 6:30 pm
Location: Peninsula

Re: passing variable to exec function

Post by beetree »

AbraCadaver wrote:You never want to trust user submitted data and don't want to pass it to exec() un-escaped: http://us2.php.net/manual/en/function.e ... ellarg.php
Re-read above stated. If you bring the code without escaping the arguments you will most likely get hacked if your site attracts any volume. Not escaping means anyone can run any command as the webserver-user.
Post Reply