tags:

views:

38

answers:

2

Hi, How do i use a php5 variable inside a system() call

$dir = '/etc/somedir';

eg system("ls $dir")

i think i'm missing something

ok sorry, i am actually passing a variable from a post eg

$username = $_POST[username];

to a system call

system("processData --user $username --pass $password");

this isn't working, so i trivialised down to a simple example

thanks

+3  A: 

You are doing it fine except that you are missing semi-colon (;)

system("ls $dir");

You can also do like:

system("ls" . $dir);

Note:

When allowing user-supplied data to be passed to this function, use escapeshellarg() or escapeshellcmd() to ensure that users cannot trick the system into executing arbitrary commands.

Sarfraz
+1 for mentioning shell security. Too many web sites have been destroyed this way.
amphetamachine
+1  A: 

it looks like you are not.. could you extend your example? are there any errors returned? what does the system() function return?

you should keep in mind that system() returns only the last line of the run command.

also, instead of 'ls' you can use a built-in php function like dir() or DirectoryIterator


kgb