0

I'm trying to create a local file using mpdf->Output() that eventually goes down to fopen() with wb as the mode.

the request always returns an error since the fopen returs false.

the folder exists with full permissions and event tried with checking the folder with is_dir and is_exists.

nothing work, and using relative and absolute path changes nothing.

the funny thing that on my local machine (windows) everything works fine and the file is saved.

the saving code:

$tmpPath = 'eventInfoFiles/EventInfo' . $reg->id . '.pdf';

$pdf->Output($tmpPath, 'F');

this is the erro message:

Invalid request - Server Error (1): Error 500: error: Unable to create output file: /var/www/html/youth-movements/api/eventInfoFiles/EventInfo207724.pdf file : /var/www/html/youth-movements/api/vendor/mpdf/mpdf/mpdf.php line : 9443

the permissions from my server. file is located at the root of the project

drwxrwxrwx.  2 root root     6 Jul  1 10:01 eventInfoFiles
Martin
  • 22,212
  • 11
  • 70
  • 132
vlad katz
  • 534
  • 3
  • 9
  • 1
    What is “file is located at the root of the project” supposed to mean? You mean the _folder_ `eventInfoFiles`? And what do you actually consider the root of the project here? – CBroe Jul 01 '20 at 07:56
  • the folder is located inside api folder (as stated in the error itself),full path is :/var/www/html/youth-movements/api (this is considered the project root folder – vlad katz Jul 01 '20 at 07:58
  • _“(as stated in the error itself)”_ - the error message states where it _couldn’t_ create the file only, we can not deduce whether the path is correct or not to begin with from that. – CBroe Jul 01 '20 at 08:09

1 Answers1

1

Use is_writable():

$path= 'eventInfoFiles/EventInfo' . $reg->id . '.pdf';
if(is_writable($path)) {
    $pdf->Output($tmpPath, 'F');
} else  {
    ...
}
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • tried it already, didn't fixed the issue – vlad katz Jul 01 '20 at 07:53
  • Isn't it usually the `www-data` group/user Apache runs on? – M. Eriksson Jul 01 '20 at 07:53
  • The username varies a lot ...may also be `www`. – Martin Zeitler Jul 01 '20 at 07:54
  • it's not the issue since i have a similar logic in another folder, that saves to another folder in the root directory with the same mpdf->Output(), and it works just fine. the second folder has the same permissions – vlad katz Jul 01 '20 at 07:55
  • the is_writable gave me the insight for fixing it. needed sudo chcon -R -t httpd_sys_rw_content_t tmp. found it from https://stackoverflow.com/questions/29343809/php-is-writable-function-always-returns-false-for-a-writable-directory. can you please edit the answer for future use. thanks – vlad katz Jul 01 '20 at 08:05
  • You didn't tell which Linux distribution you were using. – Martin Zeitler Jul 01 '20 at 11:19