Page 1 of 2

Command line funciton

Posted: Sun Jul 20, 2008 1:28 am
by JellyFish
Is there a function in php that allows me to execute a command on the command-line or shell? I remember coming across one but don't remember what the name of it was. I checked on php.net and couldn't find it. Eeek.

Is there such a thing or am I mistaken?

Re: Command line funciton

Posted: Sun Jul 20, 2008 2:41 am
by Oren
shell_exec()

Don't forget to be careful before you run this function (with user data for example).

Re: Command line funciton

Posted: Sun Jul 20, 2008 1:02 pm
by JellyFish
Awesome! Thanks.

But what's the difference between shell_exec() and exec() and even system() for that matter?

EDIT: also I can't seem to get and error if something goes wrong.

Re: Command line funciton

Posted: Sun Jul 20, 2008 1:23 pm
by Christopher
The manual explains the differences between the various functions:

http://us.php.net/manual/en/ref.exec.php

Re: Command line funciton

Posted: Sun Jul 20, 2008 1:53 pm
by JellyFish
Hmm, I can't seem to execute this:

Code: Select all

 
$output = exec("flvmdi", $output, $status);
print_r(str_replace("\n", "<br/>", $output)."<br/><br/>".$status);
 
I get the status code 127.

Re: Command line funciton

Posted: Sun Jul 20, 2008 1:56 pm
by Christopher
What OS? Can the webserver user execute that command (permission and in path)?

Re: Command line funciton

Posted: Sun Jul 20, 2008 2:20 pm
by JellyFish
Well I know that exec("ls") works and in the php info safe_mode is off. So exec() works.

I think my server is linux, I'm not sure which version of linux. It's a godaddy shared server, if your familiar with them.

Re: Command line funciton

Posted: Sun Jul 20, 2008 2:23 pm
by VladSun
Use full path to the executable file.

Re: Command line funciton

Posted: Sun Jul 20, 2008 2:39 pm
by JellyFish
I'm actually doing that too. And the same thing happens. What is 127 status mean? Why aren't I getting an error of any kind?

Re: Command line funciton

Posted: Sun Jul 20, 2008 2:46 pm
by VladSun
As arborint suggested, post the output of

Code: Select all

ls -l full/path/command
Also, to see what really happens use passthru() instead of exec() and add 2>&1 at the end of the command string.

Re: Command line funciton

Posted: Sun Jul 20, 2008 2:56 pm
by JellyFish
The output of the ls command:
-rw-r--r-- 1 username inetuser 242688 Jul 19 23:35 /path/to/flvmdi.exe

0
I changed the username and /path/to/ part. The 0 is the status code.

BTW: I'm a complete noob to linux and command lines so please bare with me.

EDIT: also I tried:

Code: Select all

 
$output = passthru("/path/to/flvmdi.exe 2>&1", $status);
print_r(str_replace("\n", "<br/>", $output)."<br/><br/>".$status);
 
and got:
sh: line 1: /home/content/t/r/a/tradingtresure/html/manager/chart-room/flvmdi.exe: Permission denied

126

Re: Command line funciton

Posted: Sun Jul 20, 2008 3:06 pm
by VladSun

Code: Select all

chmod 0755 /path/to/flvmdi.exe

Re: Command line funciton

Posted: Sun Jul 20, 2008 3:16 pm
by omniuni
I found out the hard way that shared servers don't usually like executing commands, likely for a good reason.

(I originally had written a script that used a command to delete a directory "rm -r directory", and had to replace it with a recursive PHP function)

If I may ask, what are you trying to do from the command line? It may be a good idea to instead find a way to do it using just PHP, it will certainly help with compatibility issues.

-OmniUni

Re: Command line funciton

Posted: Sun Jul 20, 2008 6:07 pm
by JellyFish
I'm trying to run a program called FLVMDI (FLV MetaData Injector) that will inject metadata information into my flv videos. I've trying searching the net for a php metadata injector but haven't found any. I was thinking of building one myself but I don't really know how that kind of data is injected/written in flvs.

Re: Command line funciton

Posted: Sun Jul 20, 2008 7:35 pm
by omniuni
This is a windows program and will not work under your Linux server, unless you configure it with WINE somehow.

I'm sorry,

-OmniUni