File handling functions file_get_contents(), file_put_contents() In Hindi

File handling functions file_get_contents(), file_put_contents() In Hindi

पीएचपी प्रोग्रामिंग में फ़ाइल हैंडलिंग कांसेप्ट के लिए fopen(), fread(), fwrite(), और fclose() फंक्शन के साथ कई और बेसिक हाई-लेवल फ़ाइल हैंडलिंग फ़ंक्शन भी यूजर को अवेलेबल मिलते हैं. जिनमे file_get_contents() और file_put_contents() फंक्शन भी शामिल है। ये फ़ंक्शन पीएचपी प्रोग्रामिंग में फ़ाइल पॉइंटर्स को मैन्युअल रूप से मैनेज किए बिना मौजूदा फ़ाइलों को रीड करने और फाइलों में कंटेंट और इनफार्मेशन को क्रिएट करने में हेल्प करते हैं।

File handling functions file_get_contents(), file_put_contents()

So, let’s take a closer look at other file handling functions in PHP programming.

file_get_contents() function in PHP.

पीएचपी फाइल हैंडलिंग प्रोसेस में file_get_contents() फंक्शन किसी भी मौजूदा फ़ाइल के कम्पलीट डाटा और इनफार्मेशन को एक स्ट्रिंग फॉर्मेट में रीड करने में हेल्प करते है। file_get_contents() फंक्शन स्पेशल आर्डर में तब यूज़फुल होते है, जब प्रोग्रामर को किसी फाइल में एक छोटी फ़ाइल को रीड करना चाहते हैं, या जब प्रोग्रामर को किसी फ़ाइल को स्माल पीसेस में प्रोसेस करने की जरूरत नहीं होती है।

Syntax of the file_get_contents() function.

$content = file_get_contents($filename, $use_include_path, $context, $offset, $maxlen);

file_get_contents() function explanation.

  • $filename – यह फ़ाइल हैंडलिंग में उस फाइल का पाथ है, जिसे यूजर रीड करना चाहते हैं।
  • $use_include_path (optional) – यह मौजूदा फ़ाइल के लिए इन्क्लुड पाथ सर्च करना है, या नहीं है। इसमें डिफ़ॉल्ट यह फाल्स है।
  • $context (optional) – एक फाइल के लिए एक रेफ़्रेन्स रिसोर्सेज है। यह फ़ाइल एक्सेस के लिए अतिरिक्त ऑप्शनल इंडीकेट करने का एक मेथड है। इसका डिफ़ॉल्ट वैल्यू जीरो है।
  • $offset (optional) – यह फाइल हैंडलिंग में वह इनिशियल पोजीशन है, जहाँ से फाइल रीडिंग स्टार्ट करना है। इसका डिफ़ॉल्ट वैल्यू 0 है।
  • $maxlen (optional) – यह फाइल हैंडलिंग में फाइल रीड करने के लिए फाइल बाइट्स की मैक्सिमम लेंथ नंबर है। इसका डिफ़ॉल्ट वैल्यू पूरी फ़ाइल है।

Example of reading a file with the PHP file_get_contents() function.

<?php

$filename = “test.txt”;

$content = file_get_contents($filename);

if ($content !== false) {

    echo “File Content is – ” . $content;

} else {

    echo “Unable to read the file content”;

}

?>

In this example.

file_get_contents() test.txt फाइल की कम्पलीट कंटेंट इनफार्मेशन को रीड करता है, और उसे $content प्रोग्राम वेरिएबल में स्टोर करता है। यदि फाइल रीड ऑपरेशन सक्सेसफुल होता है. तो यह मौजूदा फ़ाइल की कंटेंट इनफार्मेशन को आउटपुट डिस्प्ले करता है। यदि दी गई फ़ाइल रीड नहीं की जा सकती, तो यह आउटपुट फॉर्मेट में फाल्स वैल्यू को रिटर्न करता है।

Example with file_get_contents() and URL.

पीएचपी फ़ाइल हैंडलिंग में प्रोग्रामर किसी पर्टिकुलर यूआरएल जैसे, एपीआई एंडपॉइंट या वेबपेज की कंटेट को रिसीव करने के लिए file_get_contents() को भी अप्लाई कर सकते हैं.

<?php

$url = “https://www.vcanhelpsu.com”;

$content = file_get_contents($url);

if ($content !== false) {

    echo “open webPage Content – ” . $content;

} else {

    echo “unable to fetch the above URL”;

}

?>

file_put_contents() in PHP.

पीएचपी फाइल हैंडलिंग प्रोसेस में file_put_contents() का यूज़ किसी फ़ाइल में डेटा और इनफार्मेशन को क्रिएट करने में किया जाता है। यदि यहाँ फ़ाइल पहले से  स्टोरेज में मौजूद नहीं है, तो यह फंक्शन पहले उसे क्रिएट करेगा। यदि फ़ाइल पहले से मौजूद है, तो यह यूजर के द्वारा इंडीकेट फ़्लैग के आधार पर फ़ाइल को ओवरराइट कर सकता है, या उसमें कंटेंट को अपेण्ड कर सकता है।

Syntax of file_put_contents().

file_put_contents($filename, $data, $flags, $context);

file_put_contents() function explanation.

  • $filename – यह उस फ़ाइल का पाथ है, जहाँ फाइल डेटा क्रिएट किया जाएगा।
  • $data – यह वह फाइल डेटा है, जिसे यूजर फ़ाइल में क्रिएट करना चाहते हैं।
  • $flags (optional) – आप फाइल बिहैवियर को मॉडिफाई करने के लिए फ़्लैग को यूज़ कर सकते हैं। जैसे,
  • FILE_APPEND – यह मौजूदा फ़ाइल को ओवरराइट करने के बदले उस फाइल में डेटा और इनफार्मेशन को ऐड करता है।
  • LOCK_EX – यह फ़ाइल में राइट करते समय एक एक्सक्लूसिव लॉक रिसीव करता है।
  • $context (optional) – यह एक अतिरिक्त ऑप्शंस के लिए एक रेफ़्रेन्स रिसोर्सेज है। इसका डिफ़ॉल्ट वैल्यू जीरो है।

Example of writing to a file with file_put_contents().

<?php

$filename = “test.txt”;

$data = “here we add some new cotent to the file”;

$result = file_put_contents($filename, $data);

if ($result !== false) {

    echo “file Data written process success”;

} else {

    echo “file data written Failed”;

}

?>

In this example.

यहाँ file_put_contents() स्ट्रिंग $data को test.txt में क्रिएट या राइट करता है। यदि यहाँ फ़ाइल पहले से मौजूद नहीं है, तो यह पहले उस फाइल को बनाएगा। यदि यहाँ फ़ाइल पहले से मौजूद है, तो यह प्रीवियस फाइल कंटेंट को ओवरराइट कर देगा।

File example with the FILE_APPEND flag appending data.

यदि पीएचपी फ़ाइल हैंडलिंग में यूजर किसी मौजूदा फ़ाइल को ओवरराइट करने के बदले उसमें नई डिजिटल कटेंट और इनफार्मेशन को ऐड करना चाहते हैं, तो वह उसमे FILE_APPEND फंक्शन फ़्लैग को यूज़ कर सकते है.

<?php

$filename = “test.txt”;

$data = “\n let add append some addtional data to the file”;

$result = file_put_contents($filename, $data, FILE_APPEND);

if ($result !== false) {

    echo “Data appended operation success”;

} else {

    echo “data append operation Failed”;

}

?>

यहाँ, यह मौजूदा फाइल कंटेंट इनफार्मेशन को ओवरराइट करने के बदले, नया डेटा फ़ाइल के अंत में ऐड कर देगा।

Error Handling in PHP.

यदि फाइल हैंडलिंग में फाइल को प्रोसेस करते समय कोई एरर जनरेट होती है, तो file_get_contents() और file_put_contents() दोनों फंक्शन ही फाल्स वैल्यू को रिटर्न करते हैं, इसलिए, प्रॉपर फाइल हैंडलिंग एरर मैनेजमेंट के लिए रिटर्न वैल्यू को टेस्ट करना इम्पोर्टेन्ट है।

Example of error handling with the PHP file_get_contents() function.

<?php

$filename = “notexist_file.txt”;

$content = file_get_contents($filename);

if ($content === false) {

    echo “display Error to reading the file, please verify file must exist and readable.”;

}

?>

Example of error handling with file_put_contents() in PHP.

<?php

$filename = “test_file.txt”; // let predict this file is not writable format

$data = “let add some fresh content to the file”;

$output = file_put_contents($filename, $data);

if ($output === false) {

    echo “it display the Error to writing into the file, please verify the file is writable or not”;

}

?>

file_get_contents() and file_put_contents() performance considerations in PHP.

पीएचपी फ़ाइल हैंडलिंग प्रोग्रामिंग में file_get_contents() और file_put_contents() बहुत यूज़फुल फाइल हैंडलिंग फंक्शन हैं, लेकिन यह लार्ज वॉल्यूम फ़ाइलों के लिए ये डिसएबल या नॉट- कम्पेटिबल हो सकते हैं, क्योंकि ये फक्शन पूरी फ़ाइल को एक साथ रीड या राइट कर सकते हैं। यदि यूजर किसी लार्ज वॉल्यूम फ़ाइलों में कई गीगाबाइट फाइल के साथ वर्क कर रहे हैं. तो इस तरह की फ़ाइल में रीड और राइट प्रोसेस पर डिटेल्ड कण्ट्रोल के लिए fopen(), fread(), और fwrite() फंक्शन का यूज़ करना अक्सर बेहतर विकल्प होता है।

Practical use cases for file_get_contents() and file_put_contents() in PHP.

Reading a configuration file in PHP.

$config = file_get_contents(“config.json”);

$config_data = json_decode($config, true);

// Apply $config_data to application settings.

Logging data to a file in PHP.

$log_data = “Log entry: ” . date(“Y-m-d H:i:s”) . ” – User log in.”;

file_put_contents(“logfile.txt”, $log_data . PHP_EOL, FILE_APPEND);

Writing JSON data to a file in PHP.

$filedata = [“empname” => “Harry”, “email” => “harry@domain.com”];

file_put_contents(“filedata.json”, json_encode($filedata));

पीएचपी फ़ाइल हैंडलिंग प्रोग्रामिंग में file_get_contents() और file_put_contents() दोनों स्माल फ़ाइलों और जनरल यूज़ के लिए फ़ाइल मैनेजमेंट को इजी करते हैं, जिससे की वे लौ-लेवल फाइल हैंडलिंग फ़ंक्शन (fopen(), fread(), fwrite(), fclose()) के कम्पेरिज़न में अधिक इजी और कम्पेटिबल हो जाते हैं।

explanation of file_get_contents() and file_put_contents() in the file handling.

File handling FunctionFile handling Function Description
file_get_contents() functionThis function used to Reads the entire content of a file into a string format.
file_put_contents() functionThis function used to Writes data into a file, generally it appending or locking the file for put content.

A Summary of PHP File Handling Functions.

  • फ़ाइल हैंडलिंग प्रोग्रामिंग में स्माल लेवल फ़ाइलों को इमीडियेट रीड करने के लिए file_get_contents() फंक्शन को यूज़ करें।
  • फ़ाइल हैंडलिंग प्रोग्रामिंग में फाइल डेटा को डिटेल्ड आर्डर में राइट करने के लिए file_put_contents() फंक्शन को यूज़ करें।
  • फ़ाइल हैंडलिंग प्रोग्रामिंग में बड़ी फ़ाइलों के साथ पॉसिबल परफॉरमेंस इश्यूज से अलर्ट रहें। ऐसे कंडीशन में बेहतर कण्ट्रोल के लिए fopen() और fread() फंक्शन को यूज़ करें।
  • फ़ाइल हैंडलिंग प्रोग्रामिंग में इन फ़ंक्शन का यूज़ करते समय हमेशा एरर को चेक करें।

Leave a Reply