Tikky
Tikky

Reputation: 1273

How to save PDF file with mPDF and CodeIgniter

I am using CodeIgniter 3 and mPDF to generate PDF files. In my controller I need to specify file path to store pdf files.

At the moment this fragment of code works fine for me on my local server:

$pdfFilePath = FCPATH."/raports/invoice_".$data['nr'].".pdf";

however don't work on production (hosted server). As I bypass I've replaced FCPATH with $_SERVER['DOCUMENT_ROOT']

$pdfFilePath = $_SERVER['DOCUMENT_ROOT']."raports/invoice_".$data['nr'].".pdf";

This solution works on production (but not on my local server), so every time I want to upload new version I need to change it.

Error message says:

Message: fopen(): open_basedir restriction in effect. 
fopen  failed to open stream: Operation not permitted

Any solution, which could work on both local and production server?

Upvotes: 1

Views: 1763

Answers (1)

Atural
Atural

Reputation: 5439

You'll probably manage it to fix this with some kind of if statement or something like that, but there is another cool feature in CI.

In most cases you probably have more than one configuration settings which are different between Production, Testing and Development. For this reason, there is a possibility in Codeigniter to load different config values depending on which environment your platform is running.

if you use apache - activate SetEnvIf (if it itsn't already)

Put in your htaccess something like

SetEnvIf Host localhost$ CI_ENV=development
SetEnvIf Host testhost$ CI_ENV=testing
SetEnvIf Host productionhost$ CI_ENV=production

After that just simply create in your application/config/ folder three folder named development, testing and production If you put your different config files in this folders - ci accesses it depending on your current working environment

please follow the CI Documentation for further Information

Upvotes: 1

Related Questions