j0h
j0h

Reputation: 1784

How to pass arguments to executeable in windows command line program from php?

I have a program which I can execute from the command line in windows7, and pass in some arguments. The program does some calculations, and writes to a text file, then closes. No output goes to the command line.

What I need to do is execute this program via a php web-page.
I have been reading the PHP manual, and I still can't seem to get the program to run properly.

This snippet executes the EXE. Evidently, my problem is getting the arguments to be seen by the EXE.

<?php 
$du = 'Unity.exe';
echo exec($du);
echo $command;
?>

Here, where I try to pass in arguments, The EXE never runs, and the arguments are lost or something.

<?php 
//Date (no year), Time(am | pm), Name, Duration, Experiment type.
//05/05 10:00,.5,User
$du = 'C:\Apache24\old\Unity.exe';
$command = $du . ' 05/06, 1:00 AM, .5, Joh';
echo exec($command);
echo $command;
?>

I am not sure what to do. exec($command, $return_var); always comes back as 1, and no text is written to the text file.

UPDATE:

How should I pass arguments to the exe via php?
This line: $command = $du . ' 05/06, 1:00 AM, .5, Joh';

Clearly isn't working as i expected.

Upvotes: 1

Views: 920

Answers (2)

Beep.exe
Beep.exe

Reputation: 1398

here is a list of tests i ran

test.php

<?php
echo exec('echo "abc "');
echo "<br>";
echo exec('copy "test.php" "test2.php"');
echo "<br>";
echo exec('test.bat "hello this is a test"');
echo "<br>";
echo exec('test2.bat hello this is a test');
?>

test.bat

echo %1

test2.bat

@echo off
echo %1 - %2 - %3 - %4 - %5

output

"abc "
1 file(s) copied.
"hello this is a test"
hello - this - is - a - test

so, try using a bat file in the same folder as the php file for bypassing the arguments, like,

test.php

echo exec('test.bat 05/06 1:00AM .5 Joh');

test.bat

Unity.exe %1 %2 %3 %4

(and please do note that in most cases 'space' is used to separate the arguments)

Upvotes: 1

rekire
rekire

Reputation: 47985

It is possible if you use popen() it's a like fopen() but you work with command line programs. So you can read the output with fread() or fgets().

That 1 you get is the exit code or the return value of the main function which you may know from c/c++. In Linux you can read it with echo $# if I remember correctly and in Windows it is echo %errorlevel%.

Upvotes: 1

Related Questions