PHPPHP12 min read

File Handling in PHP

Read from and write to files using PHP file functions safely.

Jason Wood
October 31, 2025
6.2k173

PHP can work with files for logs, uploads, and configs.

Read file

$content = file_get_contents("data.txt");
echo $content;

Write file

file_put_contents("log.txt", "Hello
", FILE_APPEND);

Check if file exists

if (file_exists("data.txt")) {
  echo "Found";
}

Security tip

Never allow users to choose file paths directly.

Next: Uploading files from forms.

#PHP#Files#Beginner