views:

1397

answers:

6

i want to open a notepad from php file and the notepad should contain the text which i declare as string in php file. essentially a notepad should open with the text I pass from php file.

+1  A: 

It is possible to execute a program from php but only server-side.

So imagine the server runs Windows, it would start notepad server-side.

PHP gets executed on the server, and has nothing todo what's running client-side.

Sander Versluys
+10  A: 

If the PHP file is executing on a web server you cannot cause a web browser to open a new process like that. I'm sure you can imagine what a security hole that would be!

If you're running a PHP file as a local script in CLI mode, you should be able to launch notepad like any other process, e.g. using backticks or exec etc.

However, if you really wanted to do this server side, the best you could do is have a PHP script which used a Content-Disposition header, e.g.

//tell client we're delivering text
header('Content-type: text/plain');

//hint that it's a downloadable file
header('Content-Disposition: attachment; filename="textfile.txt"');

//output our text
echo "The quick brown\nfox jumps over\nthe lazy dog.";

The user can then save this file and open in their editor of choice.

Paul Dixon
A: 

First you can "create" the file and fill it with text.

Execute shell command: echo $text >> $filename then execute: notepad $filenameToOpen

Thats it.

StampedeXV
+3  A: 

You can't get PHP to open a window on the user's machine, because PHP is run entirely on the server. By the time the output reaches the browser the script will generally have terminated - you can only do what you can ask the browser to do and what it will let you (using HTML / headers etc.). For security purposes the browser will not (or should not) let an arbitrary website do very much with your machine - e.g. it will not let you spawn new Windows processes.

The best I think you could do is something like this:

$string = 'a string';

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

echo $string;

This will send the relevant headers so that the browser will treat the content as a download called file.txt of type plain text. The browser should prompt them to download a file which will be likely to open in notepad, unless they have changed the file association for .txt .

However you won't be able to get any changes back that the user makes to the document unless you ask them to upload it, so I'm not sure this is a good solution to what you are trying to acheive.

Tom Haigh
good suggestion!
Sander Versluys
Most web browsers will open text/plain themselves these days.
JoshJordan
@JoshJordan: even if you specify content-disposition:attachment etc. ?
Tom Haigh
Thats a good point. I'm not sure :) I'll have to give it a try.
JoshJordan
+1  A: 

Technically, to do this you'd have to create a file and then execute a system with that file as a parameter. Something like this:

//String to show in notepad
$myStringToDisplay = "some text to show in notepad";

//Write this string to a file
$myFile = "somefile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $myStringToDisplay);
fclose($fh);

//Execute notepad with this file as a parameter
system("notepad.exe ".$myFile);

However, this is going to execute Notepad on the server running the PHP file (if system calls are even enabled on your server), which is probably not what you want to do. PHP cannot execute any code on the client machine, and certainly cannot make a system call to execute any program it wants on the client (thank god). It would be a huge, huge security breach.

If this does not accomplish the desired functionality, please tell us what you're trying to do and why. This does not sound like a very sensible request.

JoshJordan
+1  A: 

It is not possible to open a program from your php application. But you ca load the text file using a PHP text editor. You will also be able to load the values which you are talking about.

http://www.fckeditor.net/ is one such editor.

Shoban