if (file_exists($filename)) issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
helloise
Forum Commoner
Posts: 31
Joined: Tue Sep 21, 2010 4:20 am

if (file_exists($filename)) issue

Post 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
Last edited by Weirdan on Mon Nov 08, 2010 2:38 am, edited 1 time in total.
Reason: added syntax highlighting
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: if (file_exists($filename)) issue

Post 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)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
saltwine
Forum Newbie
Posts: 16
Joined: Tue Nov 09, 2010 7:05 am
Location: London

Re: if (file_exists($filename)) issue

Post 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.
Post Reply