最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - file_exists function does not work

programmeradmin0浏览0评论
        $filecs=get_stylesheet_directory_uri().'/css/lucilevi.css';
    $filejs=get_stylesheet_directory_uri().'/js/lucilevi.js';

    if (file_exists($filecs)) {
        echo "CS found";
    } else{
        echo "CS not found";
    }

    if (file_exists($filejs)) {
        echo "JS found";
    } else{
        echo "JS not found";
    }

Although the path for directory is correct it returns false. Can Anyone explain why this happen? I am new to PHP and Wordpress. Thanks.

        $filecs=get_stylesheet_directory_uri().'/css/lucilevi.css';
    $filejs=get_stylesheet_directory_uri().'/js/lucilevi.js';

    if (file_exists($filecs)) {
        echo "CS found";
    } else{
        echo "CS not found";
    }

    if (file_exists($filejs)) {
        echo "JS found";
    } else{
        echo "JS not found";
    }

Although the path for directory is correct it returns false. Can Anyone explain why this happen? I am new to PHP and Wordpress. Thanks.

Share Improve this question edited Sep 20, 2019 at 7:57 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Sep 20, 2019 at 5:53 Lucifer LeviLucifer Levi 932 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

file_exists() lets you check if a file exists on the local server, by passing it a file path. It cannot be used to check for the existence of files via URL, and you're passing it the result of get_stylesheet_directory_uri(), which returns the URL (http:// etc.) to the file, not the path.

The proper way, these days, to get the path to a theme file is to use get_theme_file_path(), like so:

$filecs = get_theme_file_path( 'css/lucilevi.css' );
$filejs = get_theme_file_path( 'js/lucilevi.js' );

if ( file_exists( $filecs ) ) {
    // etc.
}

if ( file_exists( $filejs ) ) {
    // etc.
}

Just be aware that because $filecs and $filejs are file paths, you cannot pass them to wp_enqueue_style(), wp_enqueue_script(), you still need to pass URLs to those.

发布评论

评论列表(0)

  1. 暂无评论