0

I am having a strange issue, I will try to keep it short, but it's a bit confusing so please excuse... I have the same MakeDir command running for several pages, literally exact same code, but for some reason on one certain page the code does not work...

So a simple break down, I use the below code to create a directory if it does not already exist, and then I use mpdf to create a pdf in that directory. But for some reason I am unable to create the directory using the dynamic variables...

My code:

$directory = ROOT.'/companies/'.$current_document->company_id.'/employees/employee_'.$current_document->employee_id.'/documents/generated/signatures/'.str_replace('.pdf', '', $current_document->file_name).'/';

/*  Create Directories  */
if (!file_exists($directory)) {
   mkdir($directory, 0777, true);
}

The above code gives me an error Warning: file_exists() expects parameter 1 to be a valid path, string given and Warning: mkdir() expects parameter 1 to be a valid path, string given

But the value I get when I var_dump the variable is /home/xxxxxxx/public_html/account/companies/2/employees/employee_1233/documents/generated/signatures/LWJiVq9/ and I should mention, that using makedir with the full path instead of the variable works perfectly for some reason...

Along with that the code generates the PDF using mpdf and puts it @ /home/xxxxxxx/public_html/account/companies/2/employees/employee_1233/documents/generated/LWJiVq9.pdf in this case, but it also gets an error when generating mPDF error: Unable to create output file: /home/xxxxxxx/public_html/account/companies/2/employees/employee_1233/documents/generated/LWJiVq9.pdf

I am not sure what is causing it, because the same script generates all the other documents properly, only in this case does it completely bomb out each time...

Any Advise would be great :), Thanks guys.

Marcel
  • 874
  • 1
  • 14
  • 28

2 Answers2

0

Instead of ROOT give __DIR__ and give path relative to directory in which the script exits.

Note

DIR__The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE). This directory name does not have a trailing slash unless it is the root directory

Gireesh
  • 468
  • 7
  • 13
  • The problem with that is I work from a fixed directory, so I define root and then use it so then I can work with all includes from a constant point... i.e.`define("ROOT", realpath(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__)))))))));` (please don't crucify me haha)and then from that point I can just use ROOT as a variable... – Marcel Jan 29 '18 at 18:52
  • print the $directory value and comment the result here – Gireesh Jan 29 '18 at 19:01
  • `string(111) "/home/xxxxxxx/public_html/account/companies/2/employees/employee_1233/documents/generated/signatures/LWJiVq9/"` – Marcel Jan 29 '18 at 19:47
  • I did just notice that if I change the `LWJiVq9` to only 5 characters, then the directory is created... Any idea why? Is there a limit on the max depth of a directory on a cPanel Apache server? – Marcel Jan 29 '18 at 19:58
  • Ok so another update... It seems random some words can be 10 char long, while others are 2 and then it does not generate. I am monitoring to see if I can notice a pattern, maybe if the name starts with a number or something that it bombs out, but I can't see a pattern yet... Will update if I notice anything. – Marcel Jan 29 '18 at 20:56
  • Use **clearstatcache()** and try once again.It may solve the problem.check this [link](http://php.net/manual/en/function.file-exists.php#79308) – Gireesh Jan 30 '18 at 04:29
  • That does not seem to work either... I have been playing around. it seems to happen as soon as I include the folder name at the very end. only then.... And it's very random so I am kind of lost at how it works when I paste the directory in, but when I use a variable at the end for the last folder name, it does not work... – Marcel Feb 04 '18 at 19:34
  • @Marcel you mean it is working sometimes and sometimes not and it is behaving randomly.Did you compare all successful scenarios with failed ones. I think successful ones will not fail and i have been using MPDF for two years that i didn't face any problem till now.Also please print the path or log the path for both successful and failure scenarios and comment here.I hope you will solve your problem soon. – Gireesh Feb 05 '18 at 04:54
  • I did compare it and to the eye, it looks perfectly normal. No characters which might be causing the issue. But it seems there was "Somehow" a character in which I could not see, but it was causing the file_exist and makedir to fail on certain cases. I found a function that resolved the problem. Please see my answer... – Marcel Feb 05 '18 at 20:39
0

Ok guys, I seem to have found the issue after many an hour searching... The problem was everytime I would use the file_name variable (which would be for example 1xy.pdf the functions file_exists and makedir would fail, obviously mpdf would also not be able to create the file. But when I var_dump it out, copy and paste the entire link as is, it would work...

So I used a function to remove any illegal characters from the DB name field and it worked... But note, I also had to update the DB record again, because for some reason there is a character in there that keeps getting picked up....

The function I used was:

function normalizeString ($str = '')
{
    $str = strip_tags($str); 
    $str = preg_replace('/[\r\n\t ]+/', ' ', $str);
    $str = preg_replace('/[\"\*\/\:\<\>\?\'\|]+/', ' ', $str);
    $str = strtolower($str);
    $str = html_entity_decode( $str, ENT_QUOTES, "utf-8" );
    $str = htmlentities($str, ENT_QUOTES, "utf-8");
    $str = preg_replace("/(&)([a-z])([a-z]+;)/i", '$2', $str);
    $str = str_replace(' ', '-', $str);
    $str = rawurlencode($str);
    $str = str_replace('%', '-', $str);
    return $str;
}

which I got from string sanitizer for filename Hopefully this will help people in the future struggling... Again, no illegal characters were to be found in the DB nor in the var_dump, but it still happened, so the normalizeString is something I will keep using since this was very random, and I would recommend everyone to take a look.

Hope it helps

Marcel
  • 874
  • 1
  • 14
  • 28