Cakephp:mpdf在禁止的目录中存储生成的pdf

问题描述 投票:0回答:1

在我的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,那么它正在工作,但这样做安全吗?

php pdf cakephp mpdf phpoffice
1个回答
0
投票

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将与生成的文件分开。

© www.soinside.com 2019 - 2024. All rights reserved.