Lesson content
Read, practise, then check your understanding
PHP streams unify files and other sources. Modes control reading, append, truncation, creation, and binary semantics. Every failure and partial write must be handled.
Practical example
<?php
$handle = fopen($path, 'xb');
if ($handle === false) throw new RuntimeException('open failed');
try { if (!flock($handle, LOCK_EX) || fwrite($handle, $data) === false) throw new RuntimeException('write failed'); }
finally { fclose($handle); }
Never concatenate raw user paths into privileged roots. Constrain resolved paths, permissions, sizes, and symlink behavior. Stream large data. Important replacement often writes a temporary sibling and renames it atomically where supported.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.