Downloading doc file without saving file on server

Jun 29, 2012 at 5:18 AM

Hi everyone,

I was trying to write a code that accepts inputs from users, writes the inputs to doc, prompts download window WITHOUT saving any file on the server. I already checked the discussion board and every source code that prompts download window makes a copy of file on the server. 

<?
require_once 'PHPWord.php';
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$section->addText('Hello World');
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');	



// This will save file on the server. Is there any solution???
$objWriter->save('report.docx');
header("location:report.docx");
?>

This is the code that I have and this save file on the server. Is there any way to prompt download without saving anything to the server?

Jan 26, 2013 at 7:50 PM
Edited Jan 26, 2013 at 7:52 PM

 


$ Archivo = 'report.docx' ; / / guardar nuestro documento como este nombre de archivo de encabezado ( 'Content-Type: application / vnd.openxmlformats-officedocument.wordprocessingml.document' ); / / tipo MIME header ( 'Content-Disposition: apego; filename = "' $ nombre_archivo.. '"' ); / / indicarle al navegador cuál es el nombre del archivo de encabezado ( "Cache-Control: max-age = 0 ' ); / / no cache / / Guardar archivo objWriter $ = PHPWord_IOFactory :: createWriter ($ PHPWord, 'Word2007' );  $ ObjWriter-> save ( 'php :/ / output' );

INSERTA ESTAS lineas de Código y Asunto Resuelto

 

Jan 27, 2013 at 11:58 AM

ckj12, is saving the file to the server before download a problem? You could save a file to download with a unique name, then delete it after download. For example:

<?
require_once 'PHPWord.php';
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$section->addText('Hello World');
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');

// Save File
$temp_file_uri = tempnam('', 'xyz');
$objWriter->save($temp_file_uri);

// Download the file:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=myfile.docx');
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($temp_file_uri));
flush();
readfile($temp_file_uri);
unlink($temp_file_uri); // deletes the temporary file
exit;
?>

Feb 12, 2013 at 5:01 AM
This code is working with all browsers except IE8. Is there any way to make it possible with IE too??
May 22, 2013 at 9:26 AM
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');

$filename = 'myfile.docx';//krishna

$objWriter->save($filename);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename);
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($filename));
flush();
readfile($filename);
unlink($filename); // deletes the temporary file
exit;
May 22, 2013 at 9:27 AM
Krishna-Winnou systems and services
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');

$filename = 'myfile.docx';//krishna

$objWriter->save($filename);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename);
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($filename));
flush();
readfile($filename);
unlink($filename); // deletes the temporary file
exit;