📘 Lesson · Lesson 14
File Handling
Working with Files
PHP can create, read, write and append text files using simple functions — useful for logs, simple storage and exports.
Write to a File
<?php
$file = fopen("notes.txt", "w"); // w = write (overwrites)
fwrite($file, "Hello from PHP\n");
fclose($file);
// shortcut:
file_put_contents("log.txt", "Saved!");
?>
Read a File
<?php
$content = file_get_contents("notes.txt");
echo $content;
?>Hello from PHP
Append
<?php
file_put_contents("log.txt", "New line\n", FILE_APPEND);
?>
Summary
- fopen/fwrite/fclose or the shortcuts file_put_contents / file_get_contents.
- Use
FILE_APPENDto add without erasing.
Files के साथ काम करना
PHP simple functions से text files create, read, write और append कर सकती है — logs, simple storage और exports के लिए उपयोगी।
File में Write करें
<?php
$file = fopen("notes.txt", "w"); // w = write (overwrite)
fwrite($file, "Hello from PHP\n");
fclose($file);
// shortcut:
file_put_contents("log.txt", "Saved!");
?>
File Read करें
<?php
$content = file_get_contents("notes.txt");
echo $content;
?>Hello from PHP
Append
<?php
file_put_contents("log.txt", "New line\n", FILE_APPEND);
?>
सारांश
- fopen/fwrite/fclose या shortcuts file_put_contents / file_get_contents।
- बिना मिटाए जोड़ने को
FILE_APPENDuse करें।
💻 लाइव कोड एडिटर
इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.