file_put_contents: Failed to open stream, no such

2020-02-05 13:10发布

问题:

I am trying to use dompdf to save a form to an easily-readable .pdf file, and my processing script is below. I am receiving the error 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. (Line 39 is the final line in my full script.) I don't know how to resolve this issue.

allow_url_fopen is on, and I have tried all kinds of file paths, including the full directory (/home/username/public_html/subdir/files/grantapps/) and what's in the code below, but nothing worked.

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:

There is definitly a problem with the destination folder path.

Your above error message says, it wants to put the contents to a file in the directory /files/grantapps/, which would be beyond your vhost, but somewhere in the system (see the leading absolute slash )

You should double check:

  • Is the directory /home/username/public_html/files/grantapps/ really present.
  • Contains your loop and your file_put_contents-Statement the absolute path /home/username/public_html/files/grantapps/


回答2:

I was also stuck in the same kind of problem and i follow the simple step->

just get the exact url of the file to which u want to copy Ex->

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

and pass the exact absolute folder path with filename where do u want to write that file

1->if u r on windows machine then d:/xampp/htdocs/upload/test.txt and

2->if u r on linux machine then /var/www/html/upload/test.txt

and u can get the docuement root by the PHP function->

$_SERVER['DOCUMENT_ROOT']


标签: php dompdf