php - Checking if file exists -
i have piece of code checks whether image exists in file system , if so, displays it.
if (file_exists(realpath(dirname(__file__) . $user_image))) { echo '<img src="'.$user_image.'" />'; } else { echo "no image set"; }
if echo $user_image
out, copy , paste link browser, image there. however, here, 'no image set' being reached.
the $user_image
contents http://localhost:8888/mvc/images/users/1.jpg
some of these functions not needed?
any ideas? broken code or better way of doing (that works!)?
beside @hek2mgl answer think correct, think should switch is_file()
instead of file_exists()
. also, can go bit further like:
if(is_file(dirname(__file__). '/' . $user_image) && false !== @getimagesize(dirname(__file__) . '/'. $user_image)) { // image fine } else { // isn't }
l.e:1
oh great, telling $user_image contains? couldn't start, you? have to:
$userimagepath = parse_url($user_image, php_url_path); $fullpath = dirname(__file__) . ' / ' . $userimagepath; if($userimagepath && is_file($fullpath) && false !== @getimagesize($fullpath)) { // valid }else { // isn't }
l.e: 2 also, storing entire url not practice, happens when switch domain names? try store relative path, /blah/images/image.png
instead of http://locathost/blah/images/image.png
Comments
Post a Comment