0

I have successfully created a PDF document using FPDF but cannot get it to save to a folder on the server. I am able to output the PDF document to the browser.

I have also allowed permission to write to this folder 'fullSizeA4PdfPage':

drwxrwxrw-

My PHP code:

require('fpdf181/fpdf.php');

$pdf = new FPDF('P','pt','A4');
$pdf->AddPage();
$pdf -> Image($pathToImage, $centreX, $centreY, $imageWidth, $imageHeight);

// SAVE A4PDF FILE TO LOCAL DIR ('/fullSizeA4PdfPage')
$nameA4PDF = 'A4PdfPage.pdf';
$A4PDFPageFolder = 'fullSizeA4PdfPage';
$localPathA4PFD = $A4PDFPageFolder.'/'.$nameA4PDF;
$pdf -> Output('F', $localPathA4PFD);  // LINE 80 I have also tried $pdf -> Output($localPathA4PFD, 'F' );

I am getting the following error from the browser:

Warning: file_put_contents(fullSizeA4PdfPage/A4PdfPage.pdf): failed to open stream: No such file or directory in /var/www/html/labbook_aws/lab_server/fpdf181/fpdf.php on line 1021

Matheus Lacerda
  • 5,983
  • 11
  • 29
  • 45
eddy123
  • 43
  • 11
  • Create the `fullSizeA4PdfPage` directory manually first. – Loek May 31 '18 at 12:51
  • @fullSizeA4PdfPage the folder is already created and read/write permission granted as above – eddy123 May 31 '18 at 12:53
  • Oh that's weird. could you try `$localPathA4PDF = __DIR__ . '/fullSizeA4PdfPage/A4PdfPage.pdf';`? – Loek May 31 '18 at 12:54
  • @Loek, When I try what you suggested I get 'Undefined variable: localPathA4PFD in /var/www/html/labbook_aws/lab_server/myFile.php on line 80' – eddy123 May 31 '18 at 12:58
  • That's because 'PDF' != 'PFD'. Change the name and try again. – Loek May 31 '18 at 12:59
  • @Loek- ok corrected that- I am getting the following error: 'Warning: file_put_contents(/var/www/html/labbook_aws/lab_server/fullSizeA4PdfPage/A4PdfPage.pdf): failed to open stream: Permission denied in /var/www/html/labbook_aws/lab_server/fpdf181/fpdf.php on line 1021' – eddy123 May 31 '18 at 13:03
  • 1
    You say the permission for the directory is `rwxrwxrw-`, so non-owner non-group users cannot list the content of the directory. Maybe try 777 instead of 776 if the user running the script is not the same as the owner of the directory and not in the same group. – Karsten Koop May 31 '18 at 13:20
  • @KarstenKoop - you are correct. if you would like to submit this as an answer I can accept it – eddy123 May 31 '18 at 13:35

1 Answers1

0

With permissions of

drwxrwxrw-

processes running under a user who is not the same or in the same group as the owner cannot access the content of the directory (note the missing last x). So try permissions of 777 instead of 776 if your script runs under a different user than the owner of the directory.

See also this question for a discussion of the meaning of the x bit. It seems that even though rw should be sufficient to read and write files in a directory, without the x the meta data of the files cannot be read, so some functions may not be able to open the file at all.

Karsten Koop
  • 2,475
  • 1
  • 18
  • 23