21

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());
vaindil
  • 7,536
  • 21
  • 68
  • 127
  • A user may submit the form multiple times, and, since I'm using their name as the file name, I wanted it to just append a number to the end and save each file with a new file name rather than overwriting the existing file. – vaindil Feb 09 '13 at 00:40
  • why not use $dompdf->stream("files/grantapps/" . $name . $i . ".pdf"); – luckystars Feb 09 '13 at 00:45
  • The `stream()` function causes a download of the file and only accepts a file name as a parameter. I want to save the file on the web server, and other locations said `file_put_contents()` is the way to do it. – vaindil Feb 09 '13 at 00:49
  • For anyone having the same error message and came to this thread, in my case the issue was due to an **invalid file name** (it contained a `/`) – Samuel Gfeller Jun 25 '21 at 08:51

2 Answers2

43

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/
kenorb
  • 155,785
  • 88
  • 678
  • 743
SomeoneYouDontKnow
  • 1,289
  • 10
  • 15
  • 11
    I am the world's biggest idiot. I had done everything *except* creating the `/grantapps/` directory. Creating that and going back to the full link worked. Sorry, thank you so much! – vaindil Feb 09 '13 at 00:57
  • 2
    I have the same problem and all my paths are perfectly set up, it just throws the same error. – gegobyte Feb 19 '17 at 19:17
  • 1
    The important part for me was the use of the **absolute** path, I was using `~/`. – Tibo Aug 16 '17 at 20:38
  • For anyone having the same error message and came to this thread, in my case the issue was due to an **invalid file name** (it contained a `/`) – Samuel Gfeller Jun 25 '21 at 08:50
8

I was also stuck on the same kind of problem and I followed the simple steps below.

Just get the exact url of the file to which you want to copy, for example:

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

Then pass the exact absolute folder path with filename where you do want to write that file.

  1. If you are on a Windows machine then d:/xampp/htdocs/upload/test.txt

  2. If you are on a Linux machine then /var/www/html/upload/test.txt

You can get the document root with the PHP function $_SERVER['DOCUMENT_ROOT'].

Burgi
  • 421
  • 8
  • 24
Sachin Shukla
  • 1,249
  • 14
  • 17