How can I add custom PHP logging to a custom script or custom process (CPM)?
Process Designer, Custom Process Model (CPM), Service Process Model (SPM)
August 2016 and newer
I am receiving the following error when trying to log messages to customer portal directories in PHP:
Fatal error: Access to file_put_contents is denied because its trying to access restricted folders in /cgi-bin/interface.cfg/scripts/cp/customer/development/views/pages/script.php
A new PHP version was introduced in August 2016 version of the product. The new PHP version came with restrictions on writing to various directories on the customer portal.
For additional information about the PHP upgrade in August 2016 please review the answer below:
Answer ID 8409: PHP 5.6.18 Upgrade - General Announcement
There are still directories that are writable to in PHP. An example of such a directory is the custom scripts directory that is accessible through the File Manager in the Agent Desktop.
For testing and educational purposes, below is an example of a function that you can add to your custom script or custom process (CPM) and call it whenever you need to log a message in your code:
$fp = fopen(__DIR__ ."/scripts/custom/src/cpm_log" . date("Ymd") .".txt", "a+");
fwrite($fp, date("Y-m-d H:i:s") . " " . $string . "\n");
To access the resulting log files through the Agent Desktop Console, go to Configuration -> Site Configuration-> File Manager, select the appropriate interface and switch to "custom scripts" from the drop down selector.
If you need to create a directory in the custom scripts location (/scripts/custom/src/), you would need to create it from the code using the mkdir() PHP function. fopen() cannot create directories. Also, be aware that there is a CPM logging solution available in the product for sites with Cloud Auxiliary Storage Service (CASS) enabled. This solution is described in the product documentation here. For further details on CPM logging see Answer ID 11473: CPM logging record retention