Wednesday, 16 September 2015

How to Fix "PCLZIP_ERR_READ_OPEN_FAIL (-2) : Unable to open temporary file" Error?

If you have ever needed to ZIP files from your application, I guess, you must have heard of or used PCLZIP library. It is very simple, easy to use and feature-rich library.

If you have ever used this library on Linux, you might have come across this error stating PCLZIP_ERR_READ_OPEN_FAIL (-2) : Unable to open temporary file.

This error occurs because PCLZIP is using your current working directory to create the temporary file. But on Linux, Apache user may not have permission to write files in the current working directory so it will throw this error.

You can either give writing permission to your working directory, which I won't recommend, or you can set a temporary directory for PCLZIP.

To set temporary directory for PCLZIP, add following line to your code before you include PCLZIP Library:

define('PCLZIP_TEMPORARY_DIR', '/tmp/');

Or you can also use the path of a directory which is writable by your web server.

No comments:

Post a Comment