tags:

views:

53

answers:

2

Hi,

Or possible for users to download the file without saving it on the server? I am getting data from the database, and I want to save them .doc (MS Word) file.

if(isset($_POST['save']))
{   
$file = "new_file2.doc";
$stringData = "Text text text text...."; //This data put in new Word file

$fh = fopen($file, 'w');    
fwrite($fh, $stringData);
fclose($fh); 

header('Content-Description: File Transfer');
header('Content-type: application/msword');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);    
unlink($file);
exit;

}

How should the code look like without this: " $fh = fopen($file, 'w');
fwrite($fh, $stringData); fclose($fh);" and this "unlink($file);"

I hope to understand what I need enter code here

+4  A: 

You just need to output headers and then the content:

header(...);

echo $stringData;

No need to play with any files.

Michal Čihař
A: 

header("Content-type: application/octet-stream");

header("Content-Disposition: attachment;

filename=\"$_fileName\"");

ob_clean();

readfile($_filePath);

Mohamed Ziada