0

In my Cakephp 3.6 application I am using mpdf to create pdf files. While on localhost is working without any problem, when I try it on the server I get this error :

SplFileInfo::isFile() [https://secure.php.net/splfileinfo.isfile'>splfileinfo.isfile]: open_basedir restriction in effect. File(/tmp/mysql.sock) is not within the allowed path(s):

Thats because it is trying to save the pdf file under src folder and not under webroot.

Here is the code: (excel generator works fine)

$inv = TableRegistry::get('invoices')->get($invoice->id);   
$inv->file_id = $newFile->id;
TableRegistry::get('invoices')->save($inv);
$writer = new Xlsx($spreadsheet);
$writer->save($folder->path.'/timologio'.$inv->invoice_no.'.xlsx');



//Save as Pdf, even though $folder->path is pointing under webroot, its trying to save it under src
$PdfWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Mpdf');
$PdfWriter->save($folder->path.'/timologio'.$inv->invoice_no.'.pdf');

If I change open_basedir from {WEBSPACEROOT}{/}{:}{TMP}{/} to none then it is working but is it safe to do so?

Finwe
  • 6,372
  • 2
  • 29
  • 44
thelaw
  • 385
  • 5
  • 12

2 Answers2

1

Set temporary directory for PHPSpreadsheet PDF writer to a location you have access to:

$PdfWriter->setTempDir('path/to/your/temp/directory');
$PdfWriter->save($folder->path.'/timologio'.$inv->invoice_no.'.pdf');

As hinted at https://github.com/PHPOffice/PhpSpreadsheet/issues/1123#issuecomment-523361110

Finwe
  • 6,372
  • 2
  • 29
  • 44
0

Setting open_basedir to none is not a good idea. A better option is to add your specific storage directory to open_basedir. You can do it globally in ini file, or per directory in apache config (assuming you are using apache):

<Directory /var/www/example.domain> php_admin_value open_basedir /your/dir/here/:/another/dir/here </Directory>

Also, src folder is for your app source files, and I would not recommend to store generated pdfs there. Instead, I would make additional directory, for the sake of example lets name it storage, directly in your app directory. This way your app src will be separate from generated files.

Szymon
  • 1,385
  • 1
  • 8
  • 10