Page 1 of 1

if (file_exists($filename)) issue

Posted: Mon Nov 08, 2010 2:26 am
by helloise
please i need some help, i am new at php and dont understand the above.
my code is:

Code: Select all

<?php
           $filename = $usr_profile->getMsisdn().".png";
	   if (file_exists($filename))
            {
		$source = imagecreatefromjpeg($filename);
            }
            else
            {
                $filename = "Rainbow-code-1_blck.jpg";
		$source = imagecreatefromjpeg($filename);
                echo "<span class='spn_big_black_rbc'>You have not uploaded an image yet</span>";       
            }
        ?>
the file DOES exist but how come it executes the else? because when i refresh my web page the "You have not uploaded an image yet" wording displays
all my pics are in folder:

Code: Select all

~/traffic_2/phoenix/web/uploads/rainbowcode/images/profilepics$ ls
27823666789.png  27826597690-wap.jpg  27836001221.png  27836001238.pngjpg       Rainbow-code-1.jpg
27824578364.png  27826597693.png      27836001224.png  27836001238.png-wap.jpg  StickFig.jpg
27826117690.png  27832298298-wap.jpg  27836001238.png  Rainbow-code-1_blck.jpg
the pic in question is: 27824578364.png

please help
thanks

Re: if (file_exists($filename)) issue

Posted: Mon Nov 08, 2010 6:55 am
by s.dot
What is $source? (echo it)
The relative path may be wrong.. eg it is just coming up as /2329839832.png when it should be /uploads/rainbowcode/images/profilepics/2329839832.png

(i didn't use the actual numbers, lol)

Re: if (file_exists($filename)) issue

Posted: Tue Nov 09, 2010 8:09 am
by saltwine
Also, if you're trying to create an image resource, you need to use the correct PHP function for the file type you are working with, eg:

.png - imagecreatefrompng()
.jpeg/.jpg - imagecreatefromjpeg()

In your case you need imagecreatefrompng() for your png file. A better solution would be to read the file name (or even better the MIME type) and execute the correct code accordingly.