GitHub开源Favicon PHP脚本缓存功能异常求助
Let's tackle your two main issues one by one—first the broken cache path, then the MIME type warning.
1. Fixing the Cache Directory Write Problem
The root cause here is a critical PHP syntax mistake: the script uses + for string concatenation, but PHP requires the . operator for joining strings. Using + forces PHP to treat your path strings as numbers (which evaluate to 0), so it ends up writing files directly to the root directory instead of your specified cache folder.
Here's how to fix all the problematic lines:
Step 1: Replace all + path concatenators with .
Update these lines in your index.php file:
- Original:
@unlink('../'+$_CACHE_PATH+'/'.$domain);
Fixed:@unlink($_CACHE_PATH . '/' . $domain);(removed the extra../since you set$_CACHE_PATHto the current directory'scachefolder) - Original:
if (file_exists($_CACHE_PATH+'/'.$domain)) {
Fixed:if (file_exists($_CACHE_PATH . '/' . $domain)) { - Original:
file_put_contents($_CACHE_PATH+'/'.$domain, $favicon->icoData);
Fixed:file_put_contents($_CACHE_PATH . '/' . $domain, $favicon->icoData); - Original:
echo file_get_contents($_CACHE_PATH+'/'.$domain);
Fixed:echo file_get_contents($_CACHE_PATH . '/' . $domain);
Step 2: Ensure the cache directory exists
The script doesn't create the cache folder if it's missing—add this code right after the $_CACHE_PATH definition to auto-create the directory:
// Create cache directory if it doesn't exist if (!is_dir($_CACHE_PATH)) { mkdir($_CACHE_PATH, 0755, true); // 0755 sets standard permissions; true creates nested folders if needed }
Step 3: Sanitize domain filenames (optional but recommended)
Domain names can include characters that might cause issues on some filesystems. Add this line after defining $domain to clean up the filename:
$domain = preg_replace('/[^a-zA-Z0-9.-]/', '_', $domain);
2. Fixing the MIME Type Warning
The "Resource interpreted as Document but transferred with MIME type image/png" warning happens because the browser expects a document but receives an image, usually due to unintended whitespace output before the image data.
Here's how to resolve it:
- Remove extra whitespace: Check your
index.phpandFaviconDownloader.phpfiles for any spaces, newlines, or tabs before the opening<?phptag, or after the closing?>tag. For best practice, omit the closing?>entirely in PHP files—PHP automatically closes at the end of the file, which avoids accidental whitespace output. - Add output buffering (optional): If you're still seeing the warning, wrap your output in buffering to ensure no premature content is sent. Add
ob_start();right aftererror_reporting(0);, andob_end_flush();right before the finaldie();or at the end of the script.
Final Notes
The original script has some other rough edges (like the unaddressed user warnings you mentioned), but these fixes should resolve your immediate cache and MIME type issues. Always test changes in a non-production environment first, and consider adding error logging (replace error_reporting(0); with error_reporting(E_ALL); ini_set('display_errors', 1); temporarily to catch hidden issues).
内容的提问来源于stack exchange,提问作者Woody

