php fopen(); 本地文件上的“不接受远程主机文件访问”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28853871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
fopen(); "Remote host file access not accepted" on a local file?
提问by user2924019
I am using the Tcpdf module and PHP to create dymanic PDF invoices from an ordering system.
我正在使用 Tcpdf 模块和 PHP 从订购系统创建动态 PDF 发票。
The script should then save the invoice into a folder called "invoices". The folder exists, and there are full permissions for "everyone" (Windows).
然后脚本应将发票保存到名为“发票”的文件夹中。该文件夹存在,并且对“所有人”(Windows) 具有完全权限。
The code I am using is this:
我使用的代码是这样的:
$pdf->Output('invoices/Delivery Note.pdf', 'F');
This uses fopen to save the file.
这使用 fopen 来保存文件。
However the error I am getting is: Warning: fopen(): remote host file access not supported, file://invoices/Delivery Note.pdf
但是我得到的错误是: Warning: fopen(): remote host file access not supported, file://invoices/Delivery Note.pdf
This is a local file, not a remote one.
这是一个本地文件,而不是远程文件。
I attempted adding a / prefix like this:
我尝试添加这样的 / 前缀:
$pdf->Output('/invoices/Delivery Note.pdf', 'F');
but then I get this error instead: Warning: fopen(file:///invoices/Delivery Note.pdf): failed to open stream: No such file or directory
但后来我得到了这个错误: Warning: fopen(file:///invoices/Delivery Note.pdf): failed to open stream: No such file or directory
I created the file, and left it empty, but the same error as above.
我创建了该文件,并将其留空,但出现与上述相同的错误。
Does anyone know why I am getting this error?
有谁知道为什么我会收到这个错误?
回答by Gerd
From php-Script you can use:
从 php-Script 你可以使用:
$pdf->Output(__DIR__ . '/invoices/Delivery Note.pdf', 'F');
回答by user 1007017
After upgrading to the tcpdf 6.2.6 in vtiger 6.2 I've had the same problem, sending e-mail with pdf.
在 vtiger 6.2 中升级到 tcpdf 6.2.6 后,我遇到了同样的问题,用 pdf 发送电子邮件。
So I have changed the file:
所以我改变了文件:
libraries/tcpdf/include/tcpdf_static.php
I have commentedthe code in fopenLocal() and changed the line
我已经评论了 fopenLocal() 中的代码并更改了行
fopen($_SERVER['DOCUMENT_ROOT'].$filename, $mode);
see:
看:
/**
* 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);
}
After changing this, it worked.
更改后,它起作用了。
回答by Hilarius L. Doren
similar to user1007017
, but just comment the line like shown below (tcpdf 6.2.11)
类似于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);
}
回答by mattferderer
I suggest using the following as Gerd has also suggested but make sure you use an absolute path:
我建议按照 Gerd 的建议使用以下内容,但请确保使用绝对路径:
$pdf->Output(__DIR__ . '/invoices/Delivery Note.pdf', 'F');
$pdf->Output(__DIR__ . '/invoices/Delivery Note.pdf', 'F');
The path must be an absolute path & not a relative path. This PHP bug report explains why: https://bugs.php.net/bug.php?id=28820
路径必须是绝对路径而不是相对路径。这个 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 computerfoo
).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.
file:// 包装器不支持相对路径的原因归结为在如何处理 UNC 路径方面的妥协(更具体地说,对于 Windows 安装,如何将 / 模糊地解释为 \)。
例如:
文件://foo/bar
可以解释为相对 URI:当前工作目录中的 foo/bar,或者可以解释为 UNC:\foo\bar(
bar
在计算机上共享foo
)。由于这个原因和一些内部原因,file:// 包装器在显式调用时仅限于绝对路径。对于相对路径,要么使用 realpath() {就像您在报告中所做的那样},要么省略文件包装器的显式命名。
You can then avoid modifying the TCPDF code and worrying about any upgrades replacing your modified code.
这样您就可以避免修改 TCPDF 代码,也不必担心任何升级会替换您修改后的代码。
回答by Alejandro Enrique Lozano Enriq
In prestashop you can do it in this way $pdf->Output(_PS_ROOT_DIR_.'/modules/xxx/ticket.pdf', 'F');
在 prestashop 你可以这样做 $pdf->Output(_PS_ROOT_DIR_.'/modules/xxx/ticket.pdf', 'F');
回答by user2924019
I found the issue was that the path for fopen has to be from the document root, and not from the PHP script location.
我发现问题是 fopen 的路径必须来自文档根目录,而不是来自 PHP 脚本位置。
C:\Website\www\script\invoice\invoice.pdf
C:\Website\www\script\invoice\invoice.pdf
For example if the PHP script is inside the "script" folder, and you want to create the pdf inside the "invoice" folder, the script needs to have "\script\invoice\invoice.pdf".
例如,如果 PHP 脚本在“script”文件夹内,而您想在“invoice”文件夹内创建 pdf,则脚本需要有“\script\invoice\invoice.pdf”。
回答by Navjot Singh
try this
尝试这个
$pdf->Output($_SERVER['DOCUMENT_ROOT'].'/invoices/Delivery Note.pdf', 'F');