0
$exec = exec("net use LPT1: \\XXXXXX\PRINTER1 \\PERSISTENT:YES");
    $exec = exec("E:");
    $exec = exec("COPY OUT.TXT LPT1");

tried shell_exec

 $exec = shell_exec("net use LPT1: \\XXXXXX\PRINTER1 \\PERSISTENT:YES");
$exec = shell_exec("E:");
$exec = shell_exec("COPY OUT.TXT LPT1");

tried popen

 $exec = popen("net use LPT1: \\XXXXXX\PRINTER1 \\PERSISTENT:YES", "r");
$exec = popen("E:", "r");
$exec = popen("COPY OUT.TXT LPT1", "r");

I have tried all this in my code,but i am not able to execute it.. can anyone give a solution

dude
  • 4,532
  • 8
  • 33
  • 51
  • Debug it. See what goes wrong. The user PHP runs at may not be able to execute those commands. Also use an absolute path when copying. – Pekka Jul 11 '13 at 13:56
  • @Pekka웃 the commands works fine if i run through command prompt – dude Jul 11 '13 at 13:57
  • Your point being...? That doesn't mean PHP can run the command as well. See the source code in the question here: [How can I debug exec() problems?](http://stackoverflow.com/q/12199353) for how to make error messages appear when `exec()`ing commands. They do not show up by default. – Pekka Jul 11 '13 at 13:58
  • the user running the webserver probably doesn't have permission to do do `net use` – Orangepill Jul 11 '13 at 13:59
  • how to run this commands in php and get the output – dude Jul 11 '13 at 13:59

2 Answers2

2

exec() will not show you any error messages that your calls may produce.

To debug the problem, you need to make those messages visible. Stealing from this question:

exec('(your command here) 2>&1',$output,$return_val);
if($return_val !== 0) {
    echo 'Error<br>';
    print_r($output);   
}

Also, I'm not sure whether executing E: will actually change the working directory for the following command(s). You're probably better off using absolute paths:

 exec("COPY E:\OUT.TXT LPT1");
Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • The example is above? You just need to insert your command where it says "your command here". – Pekka Jul 11 '13 at 14:32
  • whats the variable $output and $return_val refer to in the above example – dude Jul 11 '13 at 16:38
  • You can set them to any value beforehand, they are just used to contain the return values. You can do a `$output = null; $return_val = null;` in the line before so you don't get any warnings. – Pekka Jul 11 '13 at 19:10
0

Every exec() (or other shell) call opens a shell and closes it again. You have to combine everything in a single exec() call, maybe with a batch script.

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78