How can I start a Windows GUI program using PHP? [

2019-01-15 11:44发布

Possible Duplicate:
php How do I start an external program running - Having trouble with system and exec

how to open exe with php?
I had this idea and make hard to success it for several years,but failed at last. any one tell me a success method to do the job ?

<?php 
    if(isset($_POST['file_path'])){
        /* ------- 
            using "notepad++.exe" to open "test.php" file.
            or run a bat file which calling "notepad++.exe" to open "test.php" file.
            how to seting php.ini or firefox or any setting to do this job. 
            it is only for conveniently developing web page in my PC ,not for web servers
        ------- */
    }
?>

<form action="test.php" method="post">
    <input type="text" name="file_path" value="test.php"/>
    <button type="submit">open with notepad++</button>
</form>

This would create something like:

rendered HTML screenshot

4条回答
家丑人穷心不美
2楼-- · 2019-01-15 12:14

the finally successful way I tested.
thank Charles ,refer to php How do I start an external program running - Having trouble with system and exec

  • Start->Run, type "services.msc" to bring up Services control (other ways to get there, this is easiest IMO)
  • Locate your Apache service (mine was called "wampapache" using WampServer 2.0)
  • Open the service properties (double-click or right click->properties)
  • Flip to the Log On account and ensure the checkbox titled "Allow service to interact with Desktop" is checked
  • Flip back to the General tab, stop the service, start the service

then: in php

pclose(popen("start /B \"d:\\green soft\\notepad++5.8.4\\notepad++.exe\" \"d:\\PHPnow-1.5.6\\htdocs\\laji\\a.php\"", "r"));

Thank all your good guys, what a great help . I had finally made my idea to be true. Happy new year !

查看更多
3楼-- · 2019-01-15 12:26

I assume you are wanting the client device to open Notepad++ not the remote server. If this is the case, the best you can do is to serve up the file with the proper file type header and hope the client has Notepad ++ set up as the default application to open such a file.

Something like this should do it:

header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="' . $file_name . '"'); // forces file download
header('Content-length: ' . filesize($file_path));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); // make sure file is re-validated each time it is requested

$fh = fopen($file_path, 'r');
while(!feof($fh)) {
    $buffer = fread($fh, 2048);
    echo $buffer;
}
fclose($fh);

Where $file_name is the name of the file (not the full path) and $file_path is the full path to the file

查看更多
beautiful°
4楼-- · 2019-01-15 12:31

never had a reason to do so, but have you tried passthru() ?

http://php.net/manual/en/function.passthru.php

EDIT: sorry the OP was really unclear at first glance..

what I would do is parse the file into a string or whatnot, then force the browser to treat that as a download.. php is server sided so you can`t only ask the browser to do some stuff..

$someText = 'some text here';

header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="text.txt"');

echo $someText;
查看更多
SAY GOODBYE
5楼-- · 2019-01-15 12:36

To launch a program on the computer which runs the webserver:

<?php
exec('"C:\Program Files (x86)\Notepad++\notepad++.exe" "C:\foo.php"');

The above will work on vista/win7 IF the webserver does not run as a windows service. For example, if you run apache and it automatically starts when your computer boots, you probably installed it as a service. You can check to see if apache shows up in the windows services tab/thingy.

If the webserver runs as a service, you'll need to look into enabling the "allow desktop interaction" option for the service. But otherwise:

An easy test using php's new built in webserver(php 5.4+). The key thing here is you manually start the server from a shell, so it runs as your user instead of as a service.

<?php
// C:\my\htdocs\script.php
exec('"C:\Program Files (x86)\Notepad++\notepad++.exe" "C:\foo.php"');

start a webserver via a command window

C:\path\to\php.exe -S localhost:8000 -t C:\my\htdocs

Then in your browser http://localhost:8000/script.php

查看更多
登录 后发表回答