PHP File_exist() not working - Checking if File Exist in WordPress Theme Directory

I am trying to check if a certain archive file template exists in the WP theme directory. This is my code, I am not sure what I am missing here.

?php 

$file_name = '/archive.php';
$base_template_dir = get_template_directory_uri();

$file_uri = $base_template_dir.$file_name;
$file_uri = str_replace('http://', '', $file_uri);
$file_uri = str_replace('https://', '', $file_uri);

$file_uri = str_replace($_SERVER['HTTP_HOST'], '', $file_uri);

if (file_exists($file_uri)) {
    echo 'File Found: '.$file_uri;
} else {
    echo 'File Not Found: '.$file_uri;
}
?

Topic wp-filesystem plugin-development theme-development Wordpress

Category Web


Most of this code is unnecessary. You're attempting to convert a URL to a path when you could just use the function that returns a path.

$path = get_theme_file_path( 'archive.php' );

if ( file_exists( $path ) ) {
    echo 'File Found: '. $path;
} else {
    echo 'File Not Found: '. $path;
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.