Opening, reading, writing, and closing files in PHP (fopen(), fread(), fwrite(), fclose()) In Hindi
पीएचपी वेब डेवेलपमेंट स्क्रिप्ट में, कई प्रकार के फाइल को बनाना और फ़ाइलों के साथ काम करने के लिए कुछ रिजर्व्ड फाइल हैंडलिंग कीवर्ड है, और PHP आपको फाइल मैनेजमेंट या फाइल हैंडलिंग के लिए कई बिल्ट-इन फ़ंक्शन का एक सेट प्रोवाइड करता है. जो वेब डेवलपर को मौजूदा फ़ाइलों को एफ्फिसेंटली ओपन करने, मौजूद फाइल को रीड करने, पीएचपी वेबपेज में फाइल को राइट करने और फाइल को क्लोज करने, आदि फाइल हैंडलिंग कण्ट्रोल फीचर्स मिलते है।

The basic functions used for file handling operations in PHP web development scripts are.
- fopen() – पीएचपी वेब डेवेलपमेंट स्क्रिप्ट में फ़ाइल को ओपन करते है।
- fread() – पीएचपी वेब डेवेलपमेंट स्क्रिप्ट में मौजूदा फ़ाइल से डेटा को रीड किया जाता है।
- fwrite() – पीएचपी वेब डेवेलपमेंट स्क्रिप्ट में फ़ाइल में डेटा को राइट करता है।
- fclose() – पीएचपी वेब डेवेलपमेंट स्क्रिप्ट में फाइल ऑपरेशन पूरा होने के बाद फ़ाइल को बंद किया जाता है।
Opening a file with fopen() in PHP.
पीएचपी वेबपेज में fopen() फ़ंक्शन का उपयोग पीएचपी में किसी फ़ाइल को ओपन करने में किया जाता है। किसी फाइल को ओपन करने के लिए आपको दो पैरामीटर की जरूरत होती है.
सबसे पहले उस फ़ाइल का नाम चाहिए, जिसे आप ओपन करना चाहते हैं।
मौजूदा फ़ाइल को ओपन करने का तरीका।
The syntax of file open in PHP webpage.
$handle = fopen($filename, $mode);
File control mode in PHP webpage.
$filename – मौजूदा फाइल का पथ स्टोरेज लोकेशन है।
$mode – यह एक स्ट्रिंग है, जो फ़ाइल एक्सेस मोड को इंडीकेट करता है, जैसे, फाइल को रीड करना, फाइल को राइट करना, फाइल अपेण्ड करना, आदि ऑपरेशन को डिफाइन करता है।
File handling normal mode in PHP webpage.
- ‘r’ – पीएचपी वेबपेज में फ़ाइल को रीड मोड में ओपन कर सकते है। यहाँ फ़ाइल पॉइंटर फ़ाइल की स्टार्टिंग से स्टार्ट होता है।
- ‘w’ – पीएचपी वेबपेज में फ़ाइल को राइट मोड में ओपन करता है। यहाँ यदि फ़ाइल पहले से पीएचपी वेबपेज में मौजूद है, तो इसकी डिफ़ॉल्ट कंटेंट इनफार्मेशन को रिमूव कर दी जाती है। यदि फ़ाइल मौजूद नहीं है, तो यह इसे फाइल नाम के साथ क्रिएट करता है।
- ‘a‘ – पीएचपी वेबपेज में फाइल को अपेण्ड मोड में राइट करने के लिए फ़ाइल को ओपन करता है। यदि यहाँ फ़ाइल मौजूद है, तो डेटा अंत में अपेण्ड हो जाता है। यदि पहले से फ़ाइल मौजूद नहीं है, तो यह इसे पहले क्रिएट करता है।
- ‘r+’ – पीएचपी वेबपेज में किसी फाइल को रीड और राइट करने के लिए फ़ाइल को ओपन करता है। यहाँ फ़ाइल पॉइंटर फ़ाइल को स्टार्टिंग से स्टार्ट होता है।
- ‘w+’ – पीएचपी वेबपेज में फाइल को रीड और राइट करने के लिए फ़ाइल को ओपन करता है। यदि यहाँ फ़ाइल पहले से मौजूद है, तो यह इसकी पहले से स्टोर डाटा और इनफार्मेशन कंटेंट को रिमूव कर देता है।
- ‘a+’ – पीएचपी वेबपेज में मौजूदा फाइल में डाटा और इनफार्मेशन को रीड करने और फाइल के अंत में नया डाटा और इनफार्मेशन को अपेण्ड करने के लिए फ़ाइल को अपेण्ड मोड में ओपन करते है।
Example to read file in PHP webpage.
$filename = “sample.txt”;
$handle = fopen($filename, “r”);
if ($handle) {
echo “Selected File opened successfully!”;
} else {
echo “File Unable to open.”;
}
Reading file with fread() in PHP webpage.
एक बार fopen() के साथ फ़ाइल खोली जाने के बाद, आप fread() का उपयोग करके इसकी कंटेंट इनफार्मेशन को रीड कर सकते हैं। आप फ़ाइल हैंडल और बाइट्स की संख्या पास करते हैं जिन्हें आप पढ़ना चाहते हैं।
fread() Syntax in PHP webpage.
$content = fread($handle, $length);
fread() function where.
$handle – पीएचपी वेबपेज में fopen() फाइल हैंडलिंग फंशन द्वारा लौटाया गया फ़ाइल हैंडल नाम और फाइल मोड है।
$length – पीएचपी वेबपेज में फाइल को रीड के लिए बाइट्स की संख्या दर्शाता है। यहाँ आप फ़ाइल का आकार प्राप्त करने के लिए filesize() फाइल हैंडलिंग फंक्शन का उपयोग कर सकते हैं।
Reading file in PHP webpage example.
$filename = “sample.txt”;
$handle = fopen($filename, “r”);
if ($handle) {
// Get the file size
$file_size = filesize($filename);
// Read the entire file content
$content = fread($handle, $file_size);
echo “File content: ” . $content;
fclose($handle);
} else {
echo “Unable to open file.”;
}
Writing to file with fwrite() in PHP webpage.
आप fwrite() का उपयोग करके फ़ाइल में डेटा लिख सकते हैं। यह फ़ंक्शन उस फ़ाइल हैंडल में डेटा लिखता है, जिसे आपने पहले ओपन किया था।
Syntax of fwrite() in PHP webpage.
fwrite($handle, $data);
Where in fwrite().
$handle – यह पीएचपी वेबपेज में fopen() द्वारा लौटाया गया फ़ाइल हैंडल वैल्यू है।
$data – यह पीएचपी वेबपेज में वह डेटा जिसे आप फ़ाइल में राइट करना चाहते हैं।
Writing to file in PHP webpage example.
$filename = “sample.txt”;
$handle = fopen($filename, “w”);
if ($handle) {
$data = “Let add some new content to file.”;
fwrite($handle, $data);
echo “Data write successfully!”;
fclose($handle);
} else {
echo “Unable to open file.”;
}
यहाँ इस उदाहरण में, मौजूदा फ़ाइल में कंटेंट इनफार्मेशन ओवरराइट हो जाएगी क्योंकि हमने यहाँ फ़ाइल को “w” मोड में ओपन किया है।
Appending to file in PHP webpage.
यदि आप अपने मौजूदा पीएचपी वेबपेज में फ़ाइल की कंटेंट इनफार्मेशन को ओवरराइट किए बिना एक्टिव फ़ाइल के बॉटम में फाइल डेटा और इनफार्मेशन को अपेण्ड करना चाहते हैं. तो आप फ़ाइल को एपेंड मोड (‘a’ या ‘a+’) में ओपन कर सकते हैं।
Appending to file in PHP webpage example.
$filename = “sample.txt”;
$handle = fopen($filename, “a”);
if ($handle) {
$data = “\n Here we add content at the end of the file.”;
fwrite($handle, $data);
echo “Data append operation successfully!”;
fclose($handle);
} else {
echo “Unable to open file.”;
}
यह इस एक्साम्प्ल में फ़ाइल की एक्सिस्टिंग डाटा और इनफार्मेशन को मॉडिफाई किए बिना फ़ाइल के बॉटम एन्ड में फाइल कंटेंट डेटा इनफार्मेशन को अपेण्ड कर देता है।
Closing file with fclose() in PHP webpage.
पीएचपी वेबपेज में फाइल को रीड करने या फाइल में राइट करने, जैसे फ़ाइल ऑपरेशन करने के बाद, फाइल रिसोर्सेज को एम्प्टी करने के लिए फ़ाइल को fclose() फंक्शन के साथ क्लोज किया जाता है।
fclose() syntax in PHP webpage.
fclose($handle);
fclose() in PHP webpage where.
$handle- यह मौजूदा पीएचपी वेबपेज में fopen() द्वारा लौटाया गया फ़ाइल हैंडल इनफार्मेशन है।
Example of fclose() in PHP webpage.
$filename = “sample.txt”;
$handle = fopen($filename, “r”);
// it used to Read the file content…
$content = fread($handle, filesize($filename));
echo “File content is – ” . $content;
// it used to Close the file
fclose($handle);
Complete example of reading, writing and closing a file in PHP webpage.
यहाँ आपको पीएचपी वेबपेज में एक पूरा उदाहरण मिलता है, जो फ़ाइल को ओपन करने है, फाइल कंटेंट इनफार्मेशन रीड करता है, फाइल में कंटेंट राइट करता है, और अंत में इसे बंद कर देता है।
$filename = “sample.txt”;
/// Open the file for reading and writing in PHP webpage
$handle = fopen($filename, “r+”);
if ($handle) {
// it used t Read the existing content of file
$content = fread($handle, filesize($filename));
echo “The Default Content is – ” . $content;
// it Move the file pointer to the end of the file
fseek($handle, 0, SEEK_END);
// Write new content to the file
$new_data = “\n Let add some new content to the file.”;
fwrite($handle, $new_data);
// Close the file
fclose($handle);
} else {
echo “Unable to open the file.”;
}
Error Handling
Always check if the file was successfully opened before proceeding with any reading or writing operations. For example:
$handle = fopen($filename, “r”);
if ($handle === false) {
echo “Display Error when opening the file!”;
} else {
// it allow Proceed with reading or writing file
fclose($handle);
}
Basic Summary of File Functions in file handling.
Function | Description |
fopen() | Fopen() function used to Opens a file in a specified mode. |
fread() | Fread() used to Reads data from an open file. |
fwrite() | Fwrite() used to Writes data to an open file. |
fclose() | Fclose() function used to Closes an open file after reading/writing. |
Multiple Modes for file handling fopen() operation.
Mode | Description |
r | R mode used to Open for reading only. The file pointer is placed at the beginning. |
w | W mode used to Open for writing only. If the file exists, it is overwritten. |
a | A mode used to Open for writing only. If the file exists, data is appended. |
r+ | R+ mode used to Open for reading and writing. The file pointer is placed at the beginning. |
w+ | W+ mode used to Open for reading and writing. If the file exists, it is overwritten. |
a+ | A+ mode used to Open for reading and appending operation. |
File Handling Error Management in PHP Webpage.
मौजूदा पीएचपी वेबपेज में किसी भी फाइल को रीड करने या फाइल को राइट करने के ऑपरेशन को आगे बढ़ाने से पहले हमेशा चेक कर लें कि फ़ाइल मौजूद और सफलतापूर्वक ओपन है या नहीं। जैसे,
$handle = fopen($filename, “r”);