在我的Cakephp 3.6
应用程序中,我正在使用mpdf
创建pdf文件。当在localhost上正常工作时,在服务器上尝试时出现此错误:
SplFileInfo :: isFile()[https://secure.php.net/splfileinfo.isfile'>splfileinfo.isfile]:open_basedir限制有效。 File(/tmp/mysql.sock)不在允许的路径内:
那是因为它试图将pdf文件保存在src
文件夹而不是webroot
下。
这里是代码:(excel生成器工作正常)
$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');
如果我将open_basedir
从{WEBSPACEROOT}{/}{:}{TMP}{/}
更改为none
,那么它正在工作,但这样做安全吗?
将open_basedir
设置为none
不是一个好主意。更好的选择是将您的特定存储目录添加到open_basedir
。您可以在ini文件中全局执行此操作,也可以在apache配置中的每个目录中执行此操作(假设您使用的是apache):
<Directory /var/www/example.domain>
php_admin_value open_basedir /your/dir/here/:/another/dir/here
</Directory>
也src
文件夹用于您的应用程序源文件,我不建议在此存储生成的pdf。取而代之的是,我将在您的应用程序目录中直接创建其他目录,以示例为例,将其命名为storage
。这样,您的应用程序src将与生成的文件分开。