How do file_exists look in the correct folder?

0

I'm testing if a file exists like this:

if (file_exists('assets/img/items/'. $currentLocation['id'] .'c.jpg')){
    $galleryItem .= '<img src="assets/img/items/'. $currentLocation['id'] .'c.jpg" alt="">';
}

The question is that the PHP file that is doing this query is in assets/external/ so it does not find the file.

How do I search for the correct folder?

Hugs.

    
asked by anonymous 29.12.2017 / 17:37

1 answer

1

@Denis Rudnei de Souza's comment is correct. However you can add a bit more precision (avoiding multiple file insertions, the current directory will change accidentally) using the __DIR__ constant and the realpath() function.

The DIR constant will return the script directory currently and the realpath () function will turn relative paths (../pasta1/../a .php) in absolute path.

Then applying to your situation is:

$caminho = 'assets/img/items/'. $currentLocation['id'] .'c.jpg';
if (file_exists(realpath(__DIR__ . '/../' . $caminho))){

}
    
29.12.2017 / 17:55