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

5
  • 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. Commented Jul 11, 2013 at 13:56
  • @Pekka웃 the commands works fine if i run through command prompt Commented Jul 11, 2013 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? for how to make error messages appear when exec()ing commands. They do not show up by default. Commented Jul 11, 2013 at 13:58
  • the user running the webserver probably doesn't have permission to do do net use Commented Jul 11, 2013 at 13:59
  • how to run this commands in php and get the output Commented Jul 11, 2013 at 13:59

3 Answers 3

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");
Sign up to request clarification or add additional context in comments.

3 Comments

The example is above? You just need to insert your command where it says "your command here".
whats the variable $output and $return_val refer to in the above example
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.
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.

Comments

0

I was looking for an answer to this and I found what your looking for:

Example:

shell_exec('start calc');

This will open Calculator

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.