Forum


Replies: 6   Views: 2798
Set temp_path from code
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 jhanley  · 28-10-2019 - 23:17

My web host temporary path is different for each environement (dev, stage, prod).

Leaving the temp_path setting empty in phpdocxconfig.ini causes a runtime error:

"Warning: ZipArchive::close(): Failure to create temporary file: Permission denied..."

I need the ability to set the temp_path value from code.

Does phpdocx have a method for setting environment values in code?

Posted by admin  · 29-10-2019 - 08:27

Hello,

There's no option to change it dynamically. We recommend you to add a custom INI for each environment. As an alternative approach, you can change PhpdocxUtilities.php, setting the static var as public:

public static $_phpdocxConfig;

So you can change it after CreateDocx using a new INI file:

$docx = new CreateDocx();

PhpdocxUtilities::$_phpdocxConfig = parse_ini_file('/customconfig/phpdocxconfig.ini', true);

$text = 'Lorem ipsum dolor sit amet.';
$paragraphOptions = array(
    'bold' => true,
    'font' => 'Arial'
);

$docx->addText($text, $paragraphOptions);

Or change the array attributes:

PhpdocxUtilities::$_phpdocxConfig['settings']['temp_path'] = 'new value';

We move the request to the dev team to add support to change all options dynamically in the next release of phpdocx.

Regards.

Posted by jhanley  · 30-10-2019 - 03:16

Thank you for your response.

Separate ini files is not practical, but your alternate suggestion is viable. I ordinarily don't like modifying third-party source files, but it's the best option at this point.

I made the change in PhpdocxUtilities.php as suggested. I can now modify the value of the temp_path setting in code during runtime, but this actaully revealed the crux of the problem. The temp_path isn't being recognized at all. The temporary file is actually getting created in the webroot. This works locally (where write permissions are not enforced), but fails remotely.

Even when hard-coding a temp_path setting in phpdocxconfig.ini, the temporary directory location is ignored and the file is created in the webroot.

Do you have any suggestions on how to get the temp_path to honor the value?

The document creation itself is working perfectly. I just can't get it working on production because of the aforementioned issue.

We have are using an Advanced licence in a Drupal 8 module with Acquia Cloud.

Posted by admin  · 30-10-2019 - 07:52

Hello,

Since the release of phpdocx 6.5, the library only uses the temp_path option to generate charts and download images using the HTML methods. phpdocx 6.5 and newer versions doesn't use this option to generate new documents, as they work with in-memory classes instead of generating a temp DOCX. So we understand this is your case (charts or images from HTML), or ZipArchive (PHP extension) can't work correctly with the default temp folder.

What file or directory is created in the webroot directory? A temp file or the generated DOCX? You can set any destination folder when calling createDocx using absolute or relative paths (https://www.phpdocx.com/documentation/practical/creating-a-new-documentDestination path/route section).

What value do you get when calling sys_get_temp_dir ()? Maybe you could set a custom value using PHP ini options (https://support.plesk.com/hc/en-us/articles/360001540233-How-to-change-the-directory-for-storing-temporary-PHP-files-of-a-particular-PHP-version-on-a-Plesk-server)?

If you set the correct temp_path in the INI file the temp path is used correctly? In this case you can init the config before CreateDocx is called. As you have $_phpdocxConfig set as public in PhpdocxUtilities, init the config before creating a CreateDocx object:

PhpdocxUtilities::parseConfig();
PhpdocxUtilities::$_phpdocxConfig['settings']['temp_path'] = 'new value';
$docx = new CreateDocx();

This code replaces the temp_path value before any code of phpdocx runs, so it will work in the same way than setting it using the INI file.

We understand you don't want to change the library but there's no option to change this value dynamically using the current version. A task has been added to be added in the next version of phpdocx.

Regards.

Posted by jhanley  · 30-10-2019 - 19:23

Thanks for your latest reply and continued support.

As you suspected, I am using CreateDocxFromTemplate() to create the DOCX file and createDocxAndDownload() to deliver the document immediately for download. I do not need or desire to save the documents on the server.

To answer your first question. The generated DOCX file is being saved in the webroot.

Here's the runtime error message logged on production:

Warning: ZipArchive::close(): Failure to create temporary file: Permission denied in Phpdocx\Utilities\DOCXStructure->saveDocx() 

When the "permission denied" error occurs on production, the download DOCX file is completely empty.

To answer your next question, sys_get_temp_dir() returns "/tmp" both locally and production. However, Acquia prevents the usage of "/tmp" and instead says to use "/mnt/tmp/[sitename].[env]".

https://docs.acquia.com/acquia-cloud/manage/files/temporary/

Acquia is the biggest Drupal hosting service around so compatbility with their environment is essential.

I would prefer not to mess with php.ini.

Ideally, I would like to use a custom directory as the temporary directory. In Drupal this is commonly "sites/default/files".

Again, I tried assigning "temp_path" the absolute server path to the "files" directory, but the setting is seemingly ignored by CreateDocxFromTemplate().

That said, what do you recommend to resolve this "permission denied" and temporary directory issue?

Posted by jhanley  · 30-10-2019 - 23:48

Hello again,

I solved this issue and the solution turned out to be super easy. I simply prefixed the absolute server path to the filename parameter in createDocxAndDownload() and set the second parameter to true.

I actually disovered this solution while reading the documentation page for createDocx(), where the filename pararmeter description says, "The path to the resulting Word document."

https://www.phpdocx.com/api-documentation/layout-and-general/create-and-download-docx-with-PHP

However, the filename parameter description for createDocxAndDownload() says, "File name of the document to download."

https://www.phpdocx.com/api-documentation/layout-and-general/create-and-download-docx-with-PHP

This might be obvious to some, but I mistook this to mean the filename only and led me to believe the path is defined elsewhere (like in phpdocxconfig.ini).

I would suggest changing the filename description on the createDocxAndDownload documentation page.

In any event, thanks again for the support. I'm happy to put this issue to rest.

Posted by admin  · 31-10-2019 - 07:19

Hello,

Then the problem didn't come from the temp path (needed only to generate charts and download images when importing HTML) but missing permissions to generate the DOCX output.

On https://www.phpdocx.com/documentation/practical/creating-a-new-document  you can find the documentation about working with the target output. Premium licenses also include a stream option to don't generate DOCX as file but as stream.

We move your request to the web team to consider adding more text about setting the folder output and its name.

Regards.