FILE_EXISTS函数不起作用

时间:2019-09-20 作者:Lucifer Levi

        $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";
    }
虽然目录的路径正确,但返回false。有人能解释为什么会这样吗?我是PHP和Wordpress的新手。谢谢

1 个回复
最合适的回答,由SO网友:Jacob Peattie 整理而成

file_exists() 通过向本地服务器传递文件路径,可以检查该文件是否存在于本地服务器上。它不能用于通过URL检查文件是否存在,您将通过get_stylesheet_directory_uri(), 返回文件的URL(http://etc),而不是路径。

现在,获取主题文件路径的正确方法是使用get_theme_file_path(), 像这样:

$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.
}
要知道,因为$filecs$filejs 是文件路径,您无法将其传递给wp_enqueue_style(), wp_enqueue_script(), 您仍然需要将URL传递给这些。

相关推荐

在不破坏比较修订工具的情况下通过phpMyAdmin编辑帖子的正确方法

在我网站上的数百篇帖子中,一个特定的字符串需要被另一个字符串替换,但这个替换只能在发布的帖子上执行,而不能在修订版上执行。我找到的插件(search&replace、better search replace等)没有此功能,因此必须使用phpMyAdmin完成这项工作。这可以通过此查询完成UPDATE `wp_posts` SET `post_content` = REPLACE(`post_content`, \'tobereplaced\', \'replacement\') WHERE po