Forum Moderators: coopster

Message Too Old, No Replies

download new file

         

Lolalola

7:37 pm on Apr 25, 2010 (gmt 0)



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>

Matthew1980

8:26 pm on Apr 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Lolalola,

Hopefully you are not just putting the form tags & a button in the file, you will need to have a structure there:-

<Doctype etc>
<html>
<head>
<title>
</title>
</head>
<body>
<form action="" method="post">//Just leave the action part blank as you are posting to the same page
Download: new_doc.doc,
<input type="submit" name="ok" value="OK" />
</form>
</body>
</html>

What are you trying to achieve anyway, is it just a download of a file from your server?

I assume as you have the $data (which I think maybe a reserved word somewhere along the line) var there as a debug tool of sorts?

This could be changed to this too:-

<?php
if(isset($_POST['ok']) && ($_POST['ok'] == "OK"))
{
//form submitted process data & download file..
exit;//make sure as the exit; is there or the page will be echod twice...
}

Are you saying that the contents of the .doc file has the code inside it? If it has, that certainly a new one ;)

One last observation, have you made absolutely sure as the content-type is the right one? As I am pretty sure it should be: header('Content-type: application/msword');

Check this out: [w3schools.com ] Thats a pretty good list to go from!

Cheers,
MRb

Lolalola

9:36 pm on Apr 25, 2010 (gmt 0)



Thanks ;)

Lolalola

10:45 pm on Apr 25, 2010 (gmt 0)



Sorry, but I had trouble again.
Why do I have attached everything that is opposed to "if (isset ($ _POST ['ok']) & & ($ _POST ['ok'] ==" OK "))?


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
</head>
<body>
Some text befor...
<?php
if(isset($_POST['ok']) && ($_POST['ok'] == "OK"))
{
header('Content-type: application/msword');
header("Content-Disposition: attachment; filename=New_doc.doc");
$data = "first line \n";
$data .= "second line";
echo $data;
exit;
}
?>
<form action="" method = "post">
Download: new_doc.doc,
<input type = "image" src = "button.jpg" name = "ok" value = "OK" />
</form>
Some text after...
</body>
</html>


Run script, open: New_doc.doc
"Some text befor... first line second line"
Why is my document text: "Some text befor..." ?

Matthew1980

7:20 am on Apr 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Lolalola,

Are you sure that you are using the file extension .php on the file that this code is written in, because if you use .html it wouldn't work.

One thing as I notice too is that you have removed the submit button, although the script will work, it would be better to use a submit button.

I assume as you have read: [php.net ] and that you see how to 'force' a download, after all that's what you are trying to achieve isn't it?

header('Content-type: application/msword');
header('Content-Disposition: attachment; filename="New_doc.doc"');
readfile('New_doc.doc');

Note that all the functions there are using single quotes too..

Try that, hopefully you will get what you need from that. Other than that, just make sure as the filename is correct (case sensitive) and the location is there correctly too, I suppose as you could check to see if the file_exists() before you force the download, but I would have to check that, as I have not done that part before...

Cheers,
MRb

Lolalola

11:12 am on Apr 26, 2010 (gmt 0)



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.

Matthew1980

11:53 am on Apr 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Lolalola,

Ok, I think I see what you are saying, basically you are using the vars to echo inside the .doc file that you are creating? Following so far.

Easy answer if you want to stop the html getting in:-

Place the <?php tags at the very top of the .php file :) :-

<?php
if(isset($_POST['ok']) && ($_POST['ok'] == "OK"))
{
header('Content-type: application/msword');
header("Content-Disposition: attachment; filename=New_doc.doc");
$data = "first line \n";
$data .= "second line";
echo $data;
exit;//The exit will stop the scipt echoing anything else :)
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
</head>
<body>
Some text befor...
<form action="" method = "post">
Download: new_doc.doc,
<input type = "image" src = "button.jpg" name = "ok" value = "OK" />
</form>
Some text after...
</body>
</html>


Try that, as I now see what you were trying to do :)

Cheers,
MRb

Matthew1980

5:47 pm on Apr 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Lolalola,

Right then, this code is tried and tested, works fine for me :)

This is the complete file that I just tested on my server:-

<?php
if(isset($_POST['ok']) && ($_POST['ok'] == "OK"))
{
header('Content-type: application/msword');
header('Content-Disposition: attachment; filename="New_doc.doc"');
$docText = "first line \n\r";
$docText .= "second line\n\r";
$docText .= "More text\n\r";
$docText .= "Even more text\n\r";
$docText .= "This is now working people!\n\r";

echo $docText;
exit;//The exit will stop the script echoing anything else :)
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
</head>
<body>
Top form text
<form action="" method="post">
Download the new .doc file
<input type="submit" name="ok" value="OK" />
</form>
Thanks
</body>
</html>


The only difference is that I used another name for the var & the button is a submit button. And the change of adding a newline with carriage return there too ;-p

Hope this helps you.

Cheers,
MRb