Command line funciton

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

User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Command line funciton

Post 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?
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Re: Command line funciton

Post by Oren »

shell_exec()

Don't forget to be careful before you run this function (with user data for example).
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Command line funciton

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Command line funciton

Post by Christopher »

The manual explains the differences between the various functions:

http://us.php.net/manual/en/ref.exec.php
(#10850)
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Command line funciton

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Command line funciton

Post by Christopher »

What OS? Can the webserver user execute that command (permission and in path)?
(#10850)
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Command line funciton

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Command line funciton

Post by VladSun »

Use full path to the executable file.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Command line funciton

Post 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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Command line funciton

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Command line funciton

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Command line funciton

Post by VladSun »

Code: Select all

chmod 0755 /path/to/flvmdi.exe
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Command line funciton

Post 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
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Command line funciton

Post 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.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Command line funciton

Post 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
Post Reply