• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 701
  • Last Modified:

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
0
sasnaktiv
Asked:
sasnaktiv
  • 7
  • 7
1 Solution
 
Ray PaseurCommented:
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.
0
 
sasnaktivAuthor Commented:
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
0
 
Ray PaseurCommented:
Agreed, it does not appear right.  This works on my server.  Prints Exists EXISTS
<?php // /csf/RAY_temp_sas.php
error_reporting(E_ALL);
var_dump(getcwd());
if (file_exists('images/favicon.png')) echo 'Exists';
$thing = getcwd() . DIRECTORY_SEPARATOR . 'images/favicon.png';
if (file_exists($thing)) echo 'EXISTS';

Open in new window

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
sasnaktivAuthor Commented:
It only returns a string(48) "the file path" on mine.
I'm on a UNIX server. Does that matter?
Sas
0
 
Ray PaseurCommented:
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
0
 
sasnaktivAuthor Commented:
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

0
 
Ray PaseurCommented:
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.
0
 
sasnaktivAuthor Commented:
It's hosted by GoDaddy within a shared hosting account under pharmacallmobi.
I'm not clear on how to execute your latest $xyz approach.
0
 
sasnaktivAuthor Commented:
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

0
 
Ray PaseurCommented:
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.
0
 
sasnaktivAuthor Commented:
Yes Ray,
Using error_reporting(E_ALL) is how I caught the error.
Sas
0
 
Ray PaseurCommented:
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!
0
 
sasnaktivAuthor Commented:
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
0
 
Ray PaseurCommented:
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!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now