You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP中filemtime()返回空值求助:无法获取文件修改时间

Why filemtime() Returns Empty in Your PHP Script

Hey there, let's break down why you're hitting this issue with filemtime() returning an empty value—this is a super common gotcha with WordPress theme file paths!

The Core Problem

The key mistake here is using get_stylesheet_directory_uri() to build your file path. This function returns a web-accessible URL (like http://your-site.com/wp-content/themes/your-theme/assets/playerfiles/dist/radio_player.js), but filemtime() requires a local server file system path (e.g., /var/www/html/wp-content/themes/your-theme/assets/playerfiles/dist/radio_player.js) to work correctly. Since URLs aren't valid paths on the server's filesystem, filemtime() can't find the file and returns false (which looks like an empty value in your context).

The Fix

Swap out get_stylesheet_directory_uri() with get_stylesheet_directory()—this function returns the actual local file path to your theme's directory:

$radio_file = get_stylesheet_directory() . '/assets/playerfiles/dist/radio_player.js';
$radio_ver = filemtime($radio_file);

Additional Troubleshooting Tips

If you still run into issues after making this change, check these:

  • File existence: Verify the file actually exists at the generated path using file_exists($radio_file). If this returns false, double-check your directory structure for typos.
  • Permissions: Ensure the PHP process running your WordPress site has read access to the file. Most themes use permissions like 0644 for files and 0755 for directories—if yours are stricter, adjust them accordingly.
  • Symbolic links: If the file or parent directory is a symbolic link, some server configurations might prevent filemtime() from resolving it correctly. You can try using realpath() to resolve the link first:
    $radio_file = realpath(get_stylesheet_directory() . '/assets/playerfiles/dist/radio_player.js');
    $radio_ver = filemtime($radio_file);
    

内容的提问来源于stack exchange,提问作者Gas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 13:49:06