Trouble with exec() and system()

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
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Trouble with exec() and system()

Post by The_Anomaly »

Hey guys,


I'm having a ridiculous amount of trouble with this. I was told on this forum that using the $_FILE['name']['type'] to validate that the user submitted a video is stupid, so I thought I'd use ffmpeg to check if it's a video. The following command is all I need:

Code: Select all

ffmpeg -i /path/to/video/test.wmv
So, I have the PHP code:

Code: Select all

    echo exec('ffmpeg -i /path/to/encoder/test.wmv');
It returns nothing. I've tried with more basic commands, such as ls, or something similar, and it works perfectly. I"ve tried using system() instead of exec(), but again, to no avail. Nothing returns--as in, the page is blank.

If you guys could help me out, I'd appreciate it. The long and short of it is that I can't get exec() or system() to return the output of the command. I've tried using the second parameter of exec() to populate an array, but again, the array is null.

Please note that the command works seamlessly when ran from the server, as opposed to my php script.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Trouble with exec() and system()

Post by VladSun »

Try

Code: Select all

echo system('/path/to/ffmpeg -i /path/to/encoder/test.wmv 2>&1');
PS: Though, I'm not sure what you expect to echo ...
Last edited by VladSun on Wed Jul 14, 2010 4:00 pm, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: Trouble with exec() and system()

Post by The_Anomaly »

VladSun wrote:Try

Code: Select all

echo system('/path/to/ffmpeg -i /path/to/encoder/test.wmv 2>&1');
Wow. You are the man. It freaking worked.

What on earth was this 2>&1 thing that made all the difference?

Thank you very much.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Trouble with exec() and system()

Post by VladSun »

The_Anomaly wrote:
VladSun wrote:Try

Code: Select all

echo system('/path/to/ffmpeg -i /path/to/encoder/test.wmv 2>&1');
Wow. You are the man. It freaking worked.

What on earth was this 2>&1 thing that made all the difference?

Thank you very much.
:)

0 - standard input;
1 - standard output;
2 - standard error output;

2>&1 - redirect standard error output to standard output.

system() and related functions return only standard output.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: Trouble with exec() and system()

Post by The_Anomaly »

VladSun wrote:
The_Anomaly wrote:
VladSun wrote:Try

Code: Select all

echo system('/path/to/ffmpeg -i /path/to/encoder/test.wmv 2>&1');
Wow. You are the man. It freaking worked.

What on earth was this 2>&1 thing that made all the difference?

Thank you very much.
:)

0 - standard input;
1 - standard output;
2 - standard error output;

2>&1 - redirect standard error output to standard output.

system() and related functions return only standard output.
Wow, awesome. Tell me though, is this specific to *nix? Or is it something that has to do with PHP and would have the same effect in Windows?

i.e. Does Windows have a distinction between standard and standard error output?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Trouble with exec() and system()

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
Post Reply