Click to See Complete Forum and Search --> : Newb question about saving downloaded files


MDiddy
08-04-2005, 10:27 PM
Hello

I made a small Flash Interface for generating a specific XML file. When the user is done, curently they click a button, then a text field is populated with the XML output, then they can copy & paste it into a text file.

I understand that there is a way to pass vars to a php script and have it push a file to the user. Does anyone have an example? I think I'm halfway there with this code:

<?php
$file = $_GET["file"];
$filename = $_GET["filename"];
$bytes = filesize("$file");
header("Content-type: text/xml");
header("Content-disposition: attachment; filename=\"$filename\"");
header("Content-length: $bytes");
//Read the file and output to browser
@readfile($file);
//Code to record the download in database
?>

Basically, I have code on the button in flash to post the XML var as "file" & my desired filename as "filename". The file saves for the user with the correct filename so I think I'm on the right track, but it's empty, there's no data saved.

What am I missing?

Needless to say, I've never really worked with PHP, but I understand it's syntax is similar to AS. I would really appreciate a brief exmplanation.


Thanks!

BeachSide
08-04-2005, 11:08 PM
http://www.webdeveloper.com/forum/showthread.php?t=73980&highlight=force+download

Check out Post #7

MDiddy
08-05-2005, 12:41 AM
I tried you the code, but the file is still blank. I need to get data into the file. What command does that? The filename comes through just fine, but there is no data in the file when it downloads

BeachSide
08-05-2005, 01:38 AM
Is the file already created or are you wanting to write the file and then have the user be able to download it?

MDiddy
08-05-2005, 09:04 AM
The file doesn't exist anywhere. I want to essentially take the given filename, and data for the file - and have php create the file and download it to the user.

MDiddy
08-11-2005, 08:01 PM
Anyone please?

arto
08-12-2005, 03:26 AM
readfile() function is for outputting existing files. In your case file doesn't exist, so it's useless.
If xml data is in $_GET["file"] then just print it after sending headers. In this case correct file size is length of $_GET["file"]. Something like this: <?php
$file = $_GET["file"];
$filename = $_GET["filename"];
$bytes = strlen($file);
header("Content-type: text/xml");
header("Content-disposition: attachment; filename=\"$filename\"");
header("Content-length: $bytes");

echo($file);
?> Mind you, this will send xml to browser, but it won't create file on the server. But that's what you probably want.

Arto