← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Excessive File I/O

performance PHP 5.0+ Intermediate

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

Added 15 Mar 2026
Edited 22 Mar 2026
Views 31
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 1 ping S 2 pings M 1 ping T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 2 pings S 2 pings S 0 pings M 0 pings T 0 pings W 1 ping T
No pings yesterday
Perplexity 7 Amazonbot 6 Ahrefs 5 Google 4 Unknown AI 3 SEMrush 2
crawler 23 crawler_json 3 pre-tracking 1
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

✓ schema.org compliant