file_put_contents:无法打开流,没有此类文件或目录

2022-08-31 00:11:34

我正在尝试使用dompdf将表单保存到易于阅读的.pdf文件,我的处理脚本如下。我收到错误。(第39行是我完整脚本中的最后一行。我不知道如何解决此问题。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

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());

答案 1

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

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

您应该仔细检查:

  • 目录是否确实存在。/home/username/public_html/files/grantapps/
  • 包含循环和file_put_contents语句绝对路径/home/username/public_html/files/grantapps/

答案 2

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

只需获取要复制到的文件的确切 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']


推荐