Hi,
why in my new file put all program code?
When i open "new_doc.doc", give:
"
first line
second line<form method = "post">
Download: new_doc.doc,
<input type = "submit" name = "ok" value = "OK" />
</form>
"

<?php
if(isset($_POST['ok']))
{
	header("Content-type: application/octet-stream");
	header("Content-Disposition: attachment; filename=new_doc.doc");
	$data = "first line \n";
	$data .= "second line";
	echo $data;
}
?>
<form method = "post">
	Download: new_doc.doc, 
	<input type = "submit" name = "ok" value = "OK" />
</form>

Recommended Answers

All 5 Replies

This code comes with the line "Download your_document.doc" followed by a button and when clicked will open a file download dialog asking you where to save the file or what program to use to open it (that is, in Firefox under Linux).

<?php
if (isset($_POST['ok'])) {
	header("Content-type: application/octet-stream");
	header("Content-Disposition: attachment; filename=your_document.doc");
	header("Pragma: no-cache");
	header("Expires: 0");
	readfile('your_document.doc');
}
?>
<html>
<head>
</head>
<body>
	<form method="post" action="#">
		<p>Download: your_document.doc <input type="submit" name="ok" value="OK"></p>
	</form>
</body>
</html>

Did a few test and did see if the file isn't there it will create a file with the html part in it. If the file is there the correct content will be saved.

thanks, but where put my data?
"
$data = "first line \n";
$data .= "second line";
"
?

I don't understand. Content-Disposition is used to force the browser to open the save as dialog. And as you can see, you can use html code but can't do much more in the php part, otherwise the header structure goes wrong and doesn't work anymore.

If you want your data to be saved this way, you must first place it in a file that can be saved as...

It is not possible to attach data to a existing file this way.

I see you are not completely well-understood my problem.
Probably not speak well explain.

I want to create the file(virtual), but he did not save on server.
When creating a virtual, it is presented to the user download. (and not left on the server).

That's why I use these variables:
$data = "first line \n";
$data .= "second line";

This data I get from MySQL data base.

I indeed didn't understood the problem. The code below will do what you want. Thing is, you shouldn't have any code after echo $data; If you have any code behind it, it will go in the downloaded file.

<?php
if (!isset($_POST['ok'])) {
	echo "<html>
		<head>
		</head>
		<body>
			<form method=\"post\" action=\"#\">
				<p>Download: your_document.doc <input type=\"submit\" name=\"ok\" value=\"OK\"></p>
			</form>
		</body>
		</html>";
} else {
	header("Content-type: application/octet-stream");
	header("Content-Disposition: attachment; filename=your_document.doc");
	header("Pragma: no-cache");
	header("Expires: 0");
	$data = "First line\n";
	$data .= "Second line\n";
	echo $data;
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.