PHP doesn't work with shell_exec(), system() or passthru()

自闭症网瘾萝莉.ら 提交于 2019-12-10 19:38:49

问题


I am trying to read the most recent file from the folder. After finding the latest file, I'm reading it line by line and printing it on the barcode printer using barcode Overprinter software. This barcode printer is the default printer on my server.

Here is my PHP code:

<?php
    $files = glob('C:\barcode\*.*');
    $files = array_combine($files, array_map('filectime', $files));
    arsort($files);
    echo key($files);  // for testing

    $handle = @fopen(key($files), "r");
    if ($handle) {
         while (($buffer = fgets($handle, 4096)) !== false) {
             echo $buffer; // for testing
              $retval=passthru("BarcodeGenerator.exe ".$buffer.",,Code128,0");
// based on http://www.free-barcode.com/CommandLineBarcode.htm
            echo $retval;
         }
            if (!feof($handle)) {
                echo "Error:\n";
         }
         fclose($handle);
    }
    ?>

The above command line runs fine in my command line prompt but it doesn't in php script. The program hangs. Nothing happens. If I comment the passthru line, everything is fine. The program executes.

Here is what I've tried so far:

  • Checked whether PHP is running in safe mode. It's not.
  • Turned off Windows Firewall.
  • I have run the command calcs /g BarcodeGenerator.exe everyone:F
  • I have copied the executable and the related files to the wamp\www folder. the above code (bad idea, but I still did it) and redid calcs.
  • I have changed the UAC settings to minimum
  • No antivirus is currently running on my system.
  • I've tried system(), shell_exec() and exec() too
  • I've tried turning safe mode on and putting the above directory under safe_mode_include_dir
  • I've tried replacing double quotes (") with single quotes (').
  • I've tried giving the full path (C:\\wamp\\www\\)

The commands run fine in a command prompt.

So, is there any way to run the shell_exec command as administrator under PHP? It doesn't seem to want to run an executable at all (not only this one). I'm running WAMP on Windows 7.

For example, shell_exec('dir') works fine, but shell_exec('java') doesn't. Why not?


回答1:


Since you can't run any executables, like Java, this is definitely a permissions problem. The solution, while not recommended for a production server due to security, is to run your WAMP server as administrator.

See this link for details on how to do it.



来源:https://stackoverflow.com/questions/11005831/php-doesnt-work-with-shell-exec-system-or-passthru

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!