NYCPHP Meetup

NYPHP.org

Executing from the command line

Clay Givens cjtotheg at artisanquest.com
Fri Oct 25 15:38:12 EDT 2002


Hello,
I'm trying to run a program from the command line using the passthru()
function. I'm using phptriad on an NT machine. I can get the passthru()
function to execut basic DOS commands like "dir" etc, but as soon as I try
to execute a program, my browser spins. I'm adding an extra layer even, by
running the program through a c command line parser that I wrote. Still no
good. The command line argument is executed, but it starts a rogue process
or something because the programs are running in my task manager, but the
browser never comes back.

So this is quite a situation for me now... I'm wondering if anyone has
experience running command lines from a PHP page.

Thanks a lot!
Clay

For your reference, here is my php code:

<?php

print("Click the button to execute SilkTest locally<br>");
print("<form action=\\"test.php\\" method=post>\
");
print("<input type=hidden value=1 name=go>\
");
print("<input type=submit>\
");
print("</form>\
");

if($go == 1){
	//execute local c program
	//$command = "sys_run.exe
c://progra~1/Segue/SilkTest/partner.exe";
	//$command = "/apache/htdocs/pats/sys_run.exe
/progra~1/Segue/SilkTest/partner.exe -r
/apache/htdocs/pats/pats_individual_template.t > output.txt";
	$command = "/apache/htdocs/pats/sys_run.exe
/progra~1/Segue/SilkTest/partner.exe -q -resexport -r
/apache/htdocs/pats/pats_testplan.pln";
	//$command = "/apache/htdocs/pats/sys_run.exe
/progra~1/Segue/SilkTest/partner.exe";
	//$command = "/apache/htdocs/pats/sys_run.exe dir"; //works
	//$return = exec($command);
   //$return = system($command);

	//$return = exec("c://progra~1/Segue/SilkTest/partner.exe -r
c://apache/htdocs/pats/pats_individual_template.t");
	print("<pre>");
		passthru($command);
	print("</pre>");
}

?>

and here is my c program sys_run.c that is using a system() command with
the parsed
arguments in argv[]:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char * argv[]){
	int a, b, c;
	char CmdLn[5200];

	if(argc < 2){
		printf("please specify a system command (with optional
arguments) as an argument");
		return 0;
	}

	printf("argc = %d\
", argc);

	for(a=1, b=2, c=argc; b<=c; a++, b++){
		if(b == 2){
			strcpy(CmdLn, argv[a]);
			strcat(CmdLn, " ");
		}
		if( (b > 2) && (b < c) ){
			strcat(CmdLn, argv[a]);
			strcat(CmdLn, " ");
		}
		if(b == c){
			strcat(CmdLn, argv[a]);
		}
		printf("loop: CmdLn = %s\
", CmdLn);
	}

	//"c://progra~1/Segue/SilkTest/partner.exe -r
c://apache/htdocs/pats/pats_individual_template.t"

	printf("final: CmdLn = %s\
", CmdLn);

	system(CmdLn);

	return 0;
}





More information about the talk mailing list