?
Solved

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

Posted on 2012-09-14
14
Medium Priority
?
675 Views
Last Modified: 2012-09-26
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
Comment
Question by:sasnaktiv
  • 7
  • 7
14 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38401567
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
 
LVL 1

Author Comment

by:sasnaktiv
ID: 38402140
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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 38402463
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:sasnaktiv
ID: 38406482
It only returns a string(48) "the file path" on mine.
I'm on a UNIX server. Does that matter?
Sas
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38406874
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
 
LVL 1

Author Comment

by:sasnaktiv
ID: 38407341
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38407376
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
 
LVL 1

Author Comment

by:sasnaktiv
ID: 38407408
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
 
LVL 1

Author Comment

by:sasnaktiv
ID: 38437457
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38437905
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
 
LVL 1

Author Comment

by:sasnaktiv
ID: 38438168
Yes Ray,
Using error_reporting(E_ALL) is how I caught the error.
Sas
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38438587
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
 
LVL 1

Author Closing Comment

by:sasnaktiv
ID: 38438644
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38438671
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses
Course of the Month16 days, 21 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question