简体   繁体   中英

file_exists in PHP is not working as expected

For the user's profile on my website they are allowed to upload 1 profile image. It can be PNG, JPG, JPEG, or GIF. Now my problem is displaying the images. Basically I would like to see what type of file extension it has, and then display the file accordingly. I am trying to achieve this with the file_exists function in PHP, but it does not seem to work! :(

If I type the URL (uses .png for the example)

http://localhost/postin '/profiles/pictures/username.png

into my URL bar it will display that user's image. Then if I type in the file path

C:/wamp/www/postin'/profiles/pictures/username.png

into the URL bar it will display that user's image. Now my problem is if I do PHP file_check on either of those it always says the file does NOT exist. Here is the code I am using:

<?php

$profileimagepath = "C:/wamp/www/postin'/profiles/pictures/";

$profileimageurl = "http://localhost/postin'/profiles/pictures/";

if (file_exists($profileimagepath.$username."."."png")) {   ?>
    <img src=<?php $profileimageurl.$username."."."png"; ?> id="accountpictureholderspecs">
<?php
}
elseif (file_exists($profileimagepath.$username."."."jpg")) {   ?>
    <img src=<?php $profileimageurl.$username."."."jpg"; ?> id="accountpictureholderspecs">
<?php
}
elseif (file_exists($profileimagepath.$username."."."jpeg")) {   ?>
    <img src=<?php $profileimageurl.$username."."."jpeg"; ?> id="accountpictureholderspecs">
<?php
}
elseif (file_exists($profileimagepath.$username."."."gif")) {   ?>
    <img src=<?php $profileimageurl.$username."."."gif"; ?> id="accountpictureholderspecs">
<?php
}
else {   ?>
    <img src="http://localhost/postin'/images/profileimage.png" id="accountpictureholderspecs">
<?php
}   
?>

And in the example above username is simply the user's username. So what I am wondering is why would this code not be working? Thank You!! :)

EDIT Here is my overall file path:

C:\wamp\www\postin'\profiles\pictures\image.png

Yet when I type in:

\pictures\image.png

It will not work, the image does not display! :( BTW, my directory structure:

Postin'
  Profiles
    Pictures
      image.png

From php.net comment

When using file_exists, seems you cannot do:

<?php 
foreach ($possibles as $poss) 
{ 
    if ( file_exists(SITE_RANGE_IMAGE_PATH .$this->range_id .'/ '.$poss .'.jpg') ) 
    { 
        // exists 
    } 
    else 
    { 
        // not found 
    } 
} 
?> 

so you must do:

<?php 
foreach ($possibles as $poss) 
{ 
    $img = SITE_RANGE_IMAGE_PATH .$this->range_id .'/ '.$poss .'.jpg' 
    if ( file_exists($img) ) 
    { 
        // exists 
    } 
    else 
    { 
        // not found 
    } 
} 
?> 

Then things will work fine.

This is at least the case on this Windows system running php 5.2.5 and apache 2.2.3

Not sure if it is down to the concatenation or the fact theres a constant in there, i'm about to run away and test just that...

Well, if you don't know the exact excension - don't use if/else statements. You are not able to cover every possible combination of lower-case / upper-case letters.

Personally I would recommend to convert the image into YOUR favourite format, when the user is uploading it, so you exactly know, what you are looking for!

If this is not possible for whatever reasons, you could use glob() to find any file the user uploaded.

$profileimagepath = "C:/wamp/www/postin'/profiles/pictures/";
$profileimageurl = "http://localhost/postin'/profiles/pictures/";

$files = glob($profileimagepath . $username . ".*");

foreach($files AS $file){
  echo "Found an image: <br />";
  echo $file. "<br />";
  echo "External-Adress is: " . str_replace($profileimagepath, $profileimageurl, $file);
} 

However, this would require to deal with mutliple files and ... nah... Convert it, save it, use it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM