打开(); "Remote host file access not accepted" 在本地文件上?

fopen(); "Remote host file access not accepted" on a local file?

我正在使用 Tcpdf 模块和 PHP 从订购系统创建动态 PDF 发票。

然后脚本应将发票保存到名为 "invoices" 的文件夹中。该文件夹存在,并且有 "everyone" (Windows) 的完整权限。

我使用的代码是这样的:

$pdf->Output('invoices/Delivery Note.pdf', 'F');

这使用 fopen 来保存文件。

但是我得到的错误是:Warning: fopen(): remote host file access not supported, file://invoices/Delivery Note.pdf

这是本地文件,不是远程文件。

我尝试像这样添加 / 前缀:

$pdf->Output('/invoices/Delivery Note.pdf', 'F');

但后来我得到了这个错误:Warning: fopen(file:///invoices/Delivery Note.pdf): failed to open stream: No such file or directory

我创建了文件,并将其留空,但出现与上述相同的错误。

有谁知道我为什么会收到这个错误?

试试这个

$pdf->Output($_SERVER['DOCUMENT_ROOT'].'/invoices/Delivery Note.pdf', 'F');

我发现问题是 fopen 的路径必须来自文档根目录,而不是来自 PHP 脚本位置。

C:\Website\www\script\invoice\invoice.pdf

例如,如果 PHP 脚本位于 "script" 文件夹内,而您想在 "invoice" 文件夹内创建 pdf,脚本需要具有“\script\invoice\invoice.pdf”。

在 vtiger 6.2 中升级到 tcpdf 6.2.6 后,我遇到了同样的问题,使用 pdf 发送电子邮件。

所以我更改了文件:

 libraries/tcpdf/include/tcpdf_static.php

评论 fopenLocal() 中的代码并更改了行

 fopen($_SERVER['DOCUMENT_ROOT'].$filename, $mode);

参见:

  /**
         * Wrapper to use fopen only with local files
         * @param filename (string) Name of the file to open
         * @param $mode (string) 
         * @return Returns a file pointer resource on success, or FALSE on error.  
         * @public static
         */
        public static function fopenLocal($filename, $mode) {
    //      if (strpos($filename, '://') === false) {
    //          $filename = 'file://'.$filename;
    //      } elseif (strpos($filename, 'file://') !== 0) {
    //          return false;
    //      }
            return fopen($_SERVER['DOCUMENT_ROOT'].$filename, $mode);
        }

改了之后就成功了

来自 php-您可以使用的脚本:

$pdf->Output(__DIR__ . '/invoices/Delivery Note.pdf', 'F');

类似于user1007017,但只需注释如下所示的行(tcpdf 6.2.11)

public static function fopenLocal($filename, $mode) {
        if (strpos($filename, '://') === false) {
            //$filename = 'file://'.$filename;
        } elseif (stream_is_local($filename) !== true) {
            return false;
        }
        return fopen($filename, $mode);
    }

我建议使用 Gerd 也建议的以下内容,但请确保使用绝对路径:

$pdf->Output(__DIR__ . '/invoices/Delivery Note.pdf', 'F');

路径必须是绝对路径,而不是相对路径。此 PHP 错误报告解释了原因:https://bugs.php.net/bug.php?id=28820

The reason relative paths are not supported with the file:// wrapper comes down to a compromise in how UNC paths are dealt with (and more specifically how / are fuzzily interpreted as \ for windows installations).

For Example:

file://foo/bar

Could be interpreted as a relative URI: foo/bar from the current working directory, OR it could be interpreted as a UNC: \foo\bar (share bar on computer foo).

For this and a few internal reasons the file:// wrapper is limited to absolute paths when called explicitly. For relative paths either use realpath() {as you did in your report}, or omit the explicit naming of the file wrapper.

然后您可以避免修改 TCPDF 代码并担心任何升级会替换您修改后的代码。

在 prestashop 中你可以这样做 $pdf->Output(_PS_ROOT_DIR_.'/modules/xxx/ticket.pdf', 'F');