简体   繁体   中英

download file, without saving in server

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

You just need to output headers and then the content:

header(...);

echo $stringData;

No need to play with any files.

header("Content-type: application/octet-stream");    
header("Content-Disposition: attachment; 
filename=\"$_fileName\"");
ob_clean();
readfile($_filePath);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM