PHP file_exist does not return expected results

1

I am checking if there is an image to show it if it does not exist, that it shows a default image. To prove it I have tried the following:

$imagen = 'http://'.$_SERVER['SERVER_NAME'].'/assets/img/'.$infoUsuario["USCOD"].'.png';
echo $imagen;
if(file_exists($imagen)){
    echo "EXISTE";
}else{
    echo "NO EXISTE";
}

It returns me whenever it does not exist, but if I put the route (the one shown in echo $imagen ) in the browser it shows me the image correctly.

    
asked by Lombarda Arda 05.06.2017 в 11:29
source

1 answer

4

file_exists is used to check if there is a file located on the server.

You are trying to check if the server is able to return something to you through a request http that is something different.

To verify if the image exists, you have to operate with valid file paths . An example:

$imagen = './assets/img/'.$infoUsuario["USCOD"].'.png';
if(file_exists($imagen)){
    echo "EXISTE";
}else{
    echo "NO EXISTE";
}

Note that the route I've put in the example is relative and may not work for you.

    
answered by 05.06.2017 / 11:34
source