首页 > 解决方案 > mpdf临时文件目录在服务器上传时不可写

问题描述

当我在我的文件上运行localhost它时它可以工作,但是当我使用它将它上传到我的服务器时winSCP,我收到了这个错误

PHP Fatal error: Uncaught Mpdf\MpdfException: Temporary files directory "E:\Inetpub\vhosts\gsm.org.my\httpdocs\print/custom/temp/dir/path" is not writable in E:\Inetpub\vhosts\gsm.org.my\httpdocs\print\vendor\mpdf\mpdf\src\Cache.php:17
Stack trace:
#1 E:\Inetpub\vhosts\gsm.org.my\httpdocs\print\vendor\mpdf\mpdf\src\Mpdf.php(1054): Mpdf\ServiceFactory->getServices(Object(Mpdf\Mpdf), Object(Psr\Log\NullLogger), Array, 0, Object(Mpdf\Language\LanguageToFont), Object(Mpdf\Language\ScriptToLanguage), NULL, NULL, NULL, NULL)

#2 E:\Inetpub\vhosts\gsm.org.my\httpdocs\print\print-form.php(88): Mpdf\Mpdf->__construct(Array)

#3 {main} thrown in E:\Inetpub\vhosts\gsm.org.my\httpdocs\print\vendor\mpdf\mpdf\src\Cache.php on line 17

是因为服务器找不到文件路径还是我写错了?

我尝试授予文件夹 src 的权限,但它说不能更改文件 src 的属性。我是这个领域的初学者。我尝试在谷歌上搜索有关此错误的解决方案,但我找不到任何东西。

标签: mpdf

解决方案


如果您想再试一次 mPDF:

似乎您没有为 mPDF 提供正确的配置,但我们无法确定,因为您的代码的那部分(您的第 88 行print-form.php)丢失了。取自我使用 mPDF 的上一个代码:

try {
  $mpdf = new \Mpdf\Mpdf([
    'tempDir' => __DIR__ . '/../tmp', // uses the current directory's parent "tmp" subfolder
    'setAutoTopMargin' => 'stretch',
    'setAutoBottomMargin' => 'stretch'
  ]);
} catch (\Mpdf\MpdfException $e) {
    print "Creating an mPDF object failed with" . $e->getMessage();
}

第 17 行Cache.php是 Cache 构造函数的一部分,如果临时目录不可写或不是目录,则会引发错误:

// taken from method "createBasePath($basePath)"
if (!is_writable($basePath) || !is_dir($basePath)) {
    return false;
}

要测试您是否看到由于文件权限不足或不存在的目录而导致的错误,请将包含此内容的文件上传到您的服务器并使用您首选的浏览器导航到该文件:

<?php
$pathToCheck= "E:\\Inetpub\\vhosts\\gsm.org.my\\httpdocs\\print//custom//temp//dir//path";

print 'Folder exists: '.(is_dir($pathToCheck) ? 'yes' : 'no').'<br />';
print 'Folder is writable: '.(is_writable($pathToCheck) ? 'yes' : 'no').'<br />';

您在 Windows 服务器上,因此您需要将正确的用户添加到“属性”->“安全”下的“tmp”文件夹,另外检查该文件夹是否未勾选“只读”属性。

附加建议:

请在您未来的问题中发布相关代码(如您的相关部分print-form.php),因为这样可以减少猜测可能是什么原因的风险。


推荐阅读