Web Analytics

PHP File Operations Basics

Intermediate~30 min read

File operations let you read, write, and manage files on the server. Essential for data storage, logging, and file-based applications!

Output
Click Run to execute your code

Read Entire File

<?php
$content = file_get_contents('example.txt');
echo $content;
?>

Write to File

<?php
// Overwrite file
file_put_contents('output.txt', "Hello, World!");

// Append to file
file_put_contents('output.txt', "\nNew line", FILE_APPEND);
?>

Check File Existence

<?php
if (file_exists('example.txt')) {
    echo "File exists";
}
?>

Summary

  • file_get_contents(): Read entire file
  • file_put_contents(): Write to file
  • file_exists(): Check if file exists
  • filesize(): Get file size

What's Next?

Next, learn about Advanced File Operations with fopen, fread, and fwrite!