Forum


Replies: 3   Views: 3921
Createdocxanddownload bug
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by tthrockmorton  · 02-08-2016 - 17:45

According to the documentation on the CreateDocx::createDocxAndDownload function, it is used to download the document as an output to the browser. It does this, however, I'm noticing that a duplication bug. I've attached my code below. As you can see, I'm providing the file a name of 'Policies-YYYY-MM-DD'. This name is outputted correctly and sent to the browser. However, it is also saved into the working directory where the script was executed from. I've only been able to replicate this with this name. If I change the date to be `date('Y-m-d_h:i:s')` it does not duplicate itself. I would not want to have to change the naming format of my document generation just because it causes duplication on the server. Any thoughts as to why this is happening?

require_once 'path_to_docx/classes/CreateDocx.inc';

$docx = new CreateDocxFromTemplate('filename.docx');

$docx->replaceVariableByText(array('YEAR' => date('Y')),array('target'=>'footer'));
$filename = 'Policies-'. date('Y-m-d');
$docx->createDocxAndDownload($filename);

 

Posted by admin  · 02-08-2016 - 18:10

Hello,

The method createDocxAndDownload creates and downloads the DOCX but it doesn't remove the file. If you need to remove it after being created, we recommend you to use the function unlink (http://php.net/manual/en/function.unlink.php)

About downloading name, this method just calls createDocx to generate the document and uses these lines to download it:

header(
    'Content-Type: application/vnd.openxmlformats-officedocument.' .
    'wordprocessingml.document'
);
header(
    'Content-Disposition: attachment; filename="' . $fileNameDownload .
    '.' . $this->_extension . '"'
);
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($fileName . '.' . $this->_extension));
readfile($fileName . '.' . $this->_extension);

It's a generic method to download a DOCX. If it doesn't fit your needs or it's not working on your server (some server may require additional lines to download the DOCX properly), we recommend you to use the method createDocx directly and then download it using your code, for example setting a random name in this line:

header(
  'Content-Disposition: attachment; filename="' . $fileNameDownload . '.' . $this->_extension . '"'
);

Regards.

Posted by tthrockmorton  · 02-08-2016 - 18:20

That's what I had thought when I reviewed the code, I'll have to look further into my implementation because it might be a timing problem. I had tried unlink to remove the file but it still persisted in the directory. That being said, however, this doesn't explain why it saves a working directory copy for one file name and not the other. I completely understand why it saves it inside the script directory, just don't understand how it is inconsistently consistent on when it saves it based on the file name.

Posted by admin  · 02-08-2016 - 18:28

Hello,

Maybe you could generate the DOCX with a name and set other name as download filename. Some OS may have limitations working with special characters for file names or they are escaped automatically; for example Windows doesn't allow to use ':' as part of a file name.

We have done some quick tests on a Linux server and it's working consistently.

Regards.