Skip to content
Advertisement

PHP exec() not grabbing output from bash command

i’m trying to get the ouput from a bash command. If I execute the some command directly in the bash, i see the result. But not with PHP exec() function..

$command = "ffmpeg -i '$video_path' 2>&1 | grep Video | perl -wle 'while(<>){ $_ =~ /.*?(d+xd+).*/; print $1; }'";
$res = exec($command/*, $output*/);

print_r($res);
// print_r($output);

This command try to get a video resolution using ffmpeg lib. The video path it’s fine, I triple check that.

Thanks!

EDIT: screenshot from the output in bash

enter image description here

Advertisement

Answer

Try escaping the $ in your regex with a backslash. Since you’re wrapping everything in double quotes, PHP is trying to insert the value of $_, which is not a thing. I got an undefined variable notice when I ran your code. It worked for me when I escaped the $.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement