This topic is locked

Getting content of docx file instead of attachment

5/4/2018 1:15:30 PM
PHPRunner General questions
N
NickSHDaemon author

I have php code snippet on list page Server side like this
require 'vendor/autoload.php';

$PHPWord = new \PhpOffice\PhpWord\PhpWord();

$document = $PHPWord->loadTemplate('Template.docx'); //template

$document->setValue('d_num', 'It comes from PHP !!!'); //some info

$document->saveas("1.docx");
function file_force_download($file) {

if (file_exists($file)) {

// reset the PHP output buffer to avoid overflow of the memory allocated for the script

// if this is not done the file will be read into memory completely!

if (ob_get_level()) {

ob_end_clean();

}

// force the browser to show the save file window

header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename=' . basename($file));

header('Content-Transfer-Encoding: binary');

header('Expires: 0');

header('Cache-Control: must-revalidate');

header('Pragma: public');

header('Content-Length: ' . filesize($file));

header("Content-Type: application/x-force-download; name=\"".$file."\"");

// read the file and send it to the user

readfile($file);

exit;

}

}
file_force_download("1.docx");
unlink("1.docx");
It does not work , show me contents of docx file , not downloading it.

When i put this code to some php file (run.php) in the same directory and call it from htm file with a button like <button onclick="window.location.href='run.php'">Click me</button> it working well, file is downloading correcly as a attachment.

Could somebody help me please. it works on php 5.6 apache 2.4. I already experimented with all options to let apache give files as attachment, not content, but was not succeed. It works from simple script , but does not work from phprunner snippet.

Admin 5/4/2018

You cannot do this in code snippet, you cannot display a page and at the same time send a file to download.
Just add the button code to your page in Visual Editor in HTML mode:

<button onclick="window.location.href='run.php'">Click me</button>


It should work.