Excessive File I/O
Also Known As
file I/O bottleneck
disk I/O performance
file read in loop
TL;DR
Reading the same file multiple times inside a loop or across repeated calls — avoidable with simple in-memory caching.
Explanation
File I/O is orders of magnitude slower than memory access. Reading a file once per request is fine; reading the same file inside a loop that runs 100 times per request multiplies the I/O cost 100×. The standard fix is a static cache: static $cache = []; if (!isset($cache[$key])) { $cache[$key] = file_get_contents($path); } return $cache[$key]; The first read hits disk; subsequent reads return the cached value instantly.
Common Misconception
✗ File I/O is fast enough that it rarely causes performance issues in PHP. Reading files inside loops, stat() calls on every request, and repeated fopen/fclose cycles are common PHP bottlenecks — a single file_get_contents() or APC cache read replacing repeated disk access provides significant gains.
Why It Matters
File I/O is orders of magnitude slower than memory access — reading a config file, log, or template on every request adds latency that compounds under load.
Common Mistakes
- Reading configuration files on every request instead of loading once at startup.
- Not caching file_get_contents() results for files that rarely change.
- Writing to log files synchronously on every request — use async logging or a buffer.
- Checking file_exists() in loops — each call is a filesystem syscall.
Code Examples
✗ Vulnerable
// Reading the same file on every request inside a loop
foreach ($userIds as $id) {
$config = file_get_contents('/etc/app/config.json'); // disk hit every iteration
$cfg = json_decode($config, true);
process($id, $cfg);
}
✓ Fixed
// Read once, reuse in memory
$cfg = json_decode(file_get_contents('/etc/app/config.json'), true);
foreach ($userIds as $id) {
process($id, $cfg);
}
// For cross-request reuse: OPcache can cache PHP arrays returned from files
// config/cache.php → return [...]; and use opcache_invalidate() on change
References
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
31
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 1
No pings yesterday
Perplexity 7
Amazonbot 6
Ahrefs 5
Google 4
Unknown AI 3
SEMrush 2
Also referenced
How they use it
crawler 23
crawler_json 3
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟠 High
⚙ Fix effort: Medium
⚡ Quick Fix
Cache file reads in APCu or Redis after the first request — reading the same config or CSV file on every request wastes disk I/O; measure with strace to quantify real I/O calls
📦 Applies To
PHP 5.0+
web
cli
🔗 Prerequisites
🔍 Detection Hints
config file read on every request; CSV parsed per request; file_exists in hot path; log file appended on every request sync
Auto-detectable:
✓ Yes
blackfire
strace
xdebug
⚠ Related Problems
🤖 AI Agent
Confidence: Medium
False Positives: Medium
✗ Manual fix
Fix: Medium
Context: Function
Tests: Update