PHP中filemtime()返回空值求助:无法获取文件修改时间
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 returnsfalse, 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
0644for files and0755for 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 usingrealpath()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

