Link to home
Start Free TrialLog in
Avatar of sasnaktiv
sasnaktivFlag for United States of America

asked on

TCPDF can't find file when file does exist and its path is correct.

When I use the following line of code the image appears in the PDF document properly:
$pdf->Image('logotypes/LogoNotFound.png', 0, 5, 0,9, '','','',true,300,'C');

Open in new window

However, when I use the following code the exact same file can't be found, and the else statement functions instead.
if (file_exists('logotypes/LogoNotFound.png')) {
    pdf->Image('logotypes/LogoNotFound.png', 0, 5, 0,9, '','','',true,300,'C');
} else {
    $pdf->SetFont('dejavusans', 'B', 8);
    $pdf->setXY(0,5);
    $pdf->MultiCell(89, 5, 'My Comnpany', 0, 'C', 0, 0, '', '', true);
}

Open in new window

How can I get the second set to function properly?
Please help!
 Sas
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

file_exists() may have interactions with the include path and cached information.  You may find something more useful if you explore getcwd() in conjunction with file_exists().  Suggest you isolate the file_exists() issue and test some, both with and without the DIRECTORY_SEPARATOR constant.  Consider using clearstatcache() every time, too.  Always remember that var_dump() is your friend.  The file system behaviors may be different on Windows and Linux.
Avatar of sasnaktiv

ASKER

Hi Ray,
I tried everything you suggested with no success.
This does not make sense since the first line of code I posted brings up the file correctly.
Yet the same file fails with the second code set that I posted.
Sas
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It only returns a string(48) "the file path" on mine.
I'm on a UNIX server. Does that matter?
Sas
It only returns a string(48) "the file path" on mine.
What is "it" in that context?  Can you please post the code that demonstrates this behavior and a link to the URL that contains the code? Thanks, ~Ray
Okay Ray, "It" is the following code which can be reached at:
 "http://www.gratanyc.com/RayTest.php"
Thanks,
Sas
<?php // /csf/RAY_temp_sas.php
error_reporting(E_ALL);
var_dump(getcwd());
if (file_exists('http://www.gratanyc.com/images/grata30_thumb.jpg')) echo 'Exists';
$thing = getcwd() . DIRECTORY_SEPARATOR . 'http://www.gratanyc.com/images/grata30_thumb.jpg';
if (file_exists($thing)) echo 'EXISTS';
                                            ?>

Open in new window

I think file_exists() is relative to a current working directory, not the HTTP URL.  Obviously the file exists, because I can find it here:

http://www.gratanyc.com/images/grata30_thumb.jpg

But I am a little stumped about this output:

string(52) "/home/content/p/h/a/pharmacallmobi/html/GRATANYC.COM"

I would have expected something other than GRATANYC.COM at the end of getcwd() output.  Where is this hosted?

You might want to try this one...

$xyz
= getcwd()
. DIRECTORY_SEPARATOR
. images/grata30_thumb.jpg
;

then test $xyz with fopen() or file_exists()

Sorry there is no really easy "laser-target" to fix this, but file permissions are always a little different from one server to another.
It's hosted by GoDaddy within a shared hosting account under pharmacallmobi.
I'm not clear on how to execute your latest $xyz approach.
After screwing around with this and not achieving the proper results, I went back to my original code and discovered that is was just missing the ($) on line 2. Now it's functioning properly.
Thanks
Sas
if (file_exists('logotypes/LogoNotFound.png')) {
    pdf->Image('logotypes/LogoNotFound.png', 0, 5, 0,9, '','','',true,300,'C');
} else {
    $pdf->SetFont('dejavusans', 'B', 8);
    $pdf->setXY(0,5);
    $pdf->MultiCell(89, 5, 'My Comnpany', 0, 'C', 0, 0, '', '', true);
}

Open in new window

Good grief!  Didn't that throw some kind of error or warning message?  I always use error_reporting(E_ALL) to help catch things like this.
Yes Ray,
Using error_reporting(E_ALL) is how I caught the error.
Sas
Yeah, between E_ALL and var_dump() there are not many places for errors to hide.  Unfortunately undefined properties in objects are still not flagged correctly, but maybe one day PHP will get that fixed, too!
Thanks Ray.
Now I'm struggling with placing  images off center, but centered on specific coordinates.
If that makes any sense.
I'll guess I'll have to open another ticket to address that issue.
Sas
Thanks for the points.  The good news is that with any of these PDF classes, once you get the process right, it stays right, and they run fast!