Warning: filesize(): stat failed for..error
When you encounter a Warning: filesize(): stat failed for... message in PHP, it is almost always due to a logic error where you attempt to inspect a file using a variable that hasn't been properly resolved or verified first. In your specific case, the issue lies in mixing a string path with a database column name.
Your code attempts to check if cdcovers/$album_name.jpg exists before checking its size. However, immediately after that check, you call filesize($album_name). Here, $album_name is just the filename (e.g., "Pink Floyd"), not the full path. The function expects a file path string. Because filesize() cannot find a file named literally "Pink Floyd" in the current working directory, PHP throws the "stat failed" error before it ever gets to your conditional logic regarding image size.
Correcting the Logic Flow
To fix this, you must ensure that every argument passed to filesize() is a valid absolute or relative path pointing to an actual file on the filesystem. A common pattern in PHP is to combine the directory path with the filename using string concatenation or the pathinfo functions.
Here is a corrected version of your logic that addresses the immediate error and implements your requirement to skip files larger than 5KB while downloading anything smaller:
$album_name = $row['album'];
$target_path = "cdcovers/{$album_name}.jpg"; // Ensure the .jpg extension is in the path
// Check if file exists first.
if (!file_exists($target_path)) {
// File does not exist, download it.
$imageString = LastFMArtwork::getArtwork($row['artist'], $row['album'], true, "large");
$save = file_put_contents("/home/link/public_html/cdcovers/{$album_name}.jpg", $imageString);
} else {
// File exists, check the size.
$file_size = filesize($target_path);
if ($file_size > 5000) {
// Skip if larger than 5KB
// Do nothing
} else {
// File is small (e.g., 4KB or 0KB), download a replacement.
$imageString = LastFMArtwork::getArtwork($row['artist'], $row['album'], true, "large");
file_put_contents("/home/link/public_html/cdcovers/{$album_name}.jpg", $imageString);
}
}
Handling the 0KB Edge Case
Your original requirement was to save an image even if a file exists but is 4KB or below. This includes the edge case where a placeholder file might exist with exactly 0 bytes. The filesize() function returns 0 for empty files, not false. Therefore, your condition $file_size > 5 correctly identifies both small valid images and empty placeholders as candidates for replacement.
However, be aware that if a file is 0KB, file_exists() still returns true. Relying solely on existence checks can sometimes mask permission issues or filesystem corruption where a file appears but cannot be read. Always verify the return value of filesize(). If it returns false, it means the path does not point to a readable file, regardless of whether file_exists returned true.
Performance Considerations
Checking file_exists() followed by filesize() involves two separate system calls (stat). While this is rarely a bottleneck for single requests, in high-throughput scenarios (like a crawler processing thousands of albums), you might consider using is_file() and then checking size, or utilizing lstat() if you need to handle symbolic links differently.
Another optimization strategy involves caching the result of file_exists(). If you are running this logic frequently for the same album in a loop, ensure you aren't re-querying the database unnecessarily. The code snippet above assumes $row['album'] contains the directory name or filename without spaces that break string concatenation. If your database stores paths with special characters, sanitize them before concatenating to the filesystem path to prevent unexpected behavior.
Final Thoughts on Path Resolution
The core lesson here is consistency in path resolution. Never pass a raw column value like $row['album'] directly into a file function without constructing the full path first. By explicitly building $target_path, you isolate the logic and make it clear exactly which file you are inspecting, eliminating the ambiguity that caused the stat failed error.