【问题标题】:file_put_contents: Failed to open stream, no such file or directoryfile_put_contents:打开流失败,没有这样的文件或目录
【发布时间】:2013-01-24 20:52:07
【问题描述】:

我正在尝试使用 dompdf 将表单保存到易于阅读的 .pdf 文件中,我的处理脚本如下。我收到错误Warning: file_put_contents(/files/grantapps/NAME0.pdf) [function.file-put-contents]: failed to open stream: No such file or directory in /home/username/public_html/subdir/apps/dompdf/filename.php on line 39。 (第 39 行是我完整脚本的最后一行。)我不知道如何解决这个问题。

allow_url_fopen 已打开,我尝试了各种文件路径,包括完整目录 (/home/username/public_html/subdir/files/grantapps/) 和下面代码中的内容,但没有任何效果。

require_once("dompdf_config.inc.php");
date_default_timezone_set('America/Detroit');

$html = 'code and whatnot is here';

$name = str_replace(" ", "", $_POST['form2name']);
$i = 0;
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
while(file_exists("files/grantapps/" . $name . $i . ".pdf")) {
    $i++;
}
file_put_contents("files/grantapps/" . $name . $i . ".pdf", $dompdf->output());

【问题讨论】:

  • 一个用户可以多次提交表单,并且由于我使用他们的名字作为文件名,我希望它只是在末尾附加一个数字并将每个文件保存为一个新文件命名而不是覆盖现有文件。
  • 为什么不使用 $dompdf->stream("files/grantapps/" . $name . $i . ".pdf");
  • stream() 函数会导致下载文件,并且只接受文件名作为参数。我想将文件保存在网络服务器上,其他位置说file_put_contents()是这样做的。
  • 对于任何有相同错误消息并来到此线程的人,在我的情况下,问题是由于无效文件名(它包含/)跨度>

标签: php dompdf


【解决方案1】:

我也遇到了同样的问题,我按照下面的简单步骤操作。

只需获取要复制到的文件的确切 url,例如:

http://www.test.com/test.txt (file to copy)

然后传递确切的带有文件名的绝对文件夹路径,您确实要在其中写入该文件。

  1. 如果您使用的是 Windows 机器,那么 d:/xampp/htdocs/upload/test.txt

  2. 如果您使用的是 Linux 机器,那么 /var/www/html/upload/test.txt

您可以使用 PHP 函数 $_SERVER['DOCUMENT_ROOT'] 获取文档根目录。

【讨论】:

    【解决方案2】:

    目标文件夹路径肯定有问题。

    您上面的错误消息说,它想将内容放到目录/files/grantapps/ 中的文件中,这将超出您的vhost,但在系统中的某个位置(请参阅前导绝对斜杠)

    您应该仔细检查:

    • 目录/home/username/public_html/files/grantapps/ 真的存在吗?
    • 包含你的循环和你的file_put_contents-Statement绝对路径/home/username/public_html/files/grantapps/

    【讨论】:

    • 我是世界上最大的白痴。我已经完成了所有除了创建/grantapps/ 目录的工作。创建它并返回完整链接是有效的。对不起,非常感谢!
    • 我有同样的问题,我所有的路径都设置得很好,它只是抛出同样的错误。
    • 对我来说重要的部分是使用 absolute 路径,我使用的是~/
    • 对于任何有相同错误消息并来到此线程的人,在我的情况下,问题是由于 无效文件名(它包含 /)跨度>
    猜你喜欢
    • 2018-12-01
    • 2020-01-12
    • 2014-09-05
    • 1970-01-01
    • 2019-06-03
    • 2018-08-30
    • 2018-03-26
    • 2022-10-16
    • 1970-01-01
    相关资源
    最近更新 更多