Skip to content
Learn Netverks

Lesson

Step 31/36 86% through track

file-io-basics

File I/O basics

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches File I/O basics: the syntax, APIs, and habits you need before advancing in PHP.

Teams ship File I/O basics on every PHP codebase—skipping it leaves gaps in debugging and code reviews.

You will apply File I/O basics in contexts like: LAMP/LEMP stacks, Laravel apps, WordPress themes/plugins, and shared hosting.

Write PHP in the editor and click Run on server—the dev runner executes your script and returns stdout/stderr (set LEARNING_RUNNER_ENABLED=true locally).

Toward the end of the track—consolidate before capstone-style review lessons.

PHP reads and writes files for logs, caches, uploads, and imports. Know safe paths, permissions, and when to use streams for large files.

Reading and writing

$text = file_get_contents('/path/to/file.txt');
file_put_contents('/path/to/out.json', $json, LOCK_EX);

$handle = fopen('/path/to/data.csv', 'r');
while (($line = fgets($handle)) !== false) {
    // process line
}
fclose($handle);

Uploads

$_FILES contains temp paths, sizes, and MIME types. Move with move_uploaded_file to a non-executable directory; validate type and size server-side.

Safety

  • Never use user input directly as filesystem paths (path traversal)
  • Store uploads outside web root or disable script execution
  • Use realpath and prefix checks for allowed directories

Self-check

  1. Why LOCK_EX on concurrent writes?
  2. What function moves an uploaded temp file?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Upload validation?
  • realpath check?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump