PHP फ़ाइल खोलें/पढ़ें/बंद करें
इस अध्याय में हम आपको सर्वर पर फाइल को खोलना, पढ़ना और बंद करना सिखाएंगे।
पीएचपी ओपन फाइल - फॉपेन ()
फ़ाइलों को खोलने का एक बेहतर तरीका fopen()
फ़ंक्शन के साथ है। यह फ़ंक्शन आपको फ़ंक्शन से अधिक विकल्प देता है readfile()
।
हम पाठ फ़ाइल, "webdictionary.txt" का प्रयोग पाठों के दौरान करेंगे:
AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language
के पहले पैरामीटर में fopen()
खोली जाने वाली फ़ाइल का नाम होता है और दूसरा पैरामीटर निर्दिष्ट करता है कि फ़ाइल को किस मोड में खोला जाना चाहिए। यदि fopen () फ़ंक्शन निर्दिष्ट फ़ाइल को खोलने में असमर्थ है, तो निम्न उदाहरण भी एक संदेश उत्पन्न करता है:
उदाहरण
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
युक्ति: और कार्यों fread()
को fclose()
नीचे समझाया जाएगा।
फ़ाइल को निम्न में से किसी एक मोड में खोला जा सकता है:
Modes | Description |
---|---|
r | Open a file for read only. File pointer starts at the beginning of the file |
w | Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
a | Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x | Creates a new file for write only. Returns FALSE and an error if file already exists |
r+ | Open a file for read/write. File pointer starts at the beginning of the file |
w+ | Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
a+ | Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x+ | Creates a new file for read/write. Returns FALSE and an error if file already exists |
PHP फ़ाइल पढ़ें - फ़्रेड ()
fread()
फ़ंक्शन एक खुली फ़ाइल से पढ़ता है ।
के पहले पैरामीटर में fread()
पढ़ने के लिए फ़ाइल का नाम होता है और दूसरा पैरामीटर पढ़ने के लिए बाइट्स की अधिकतम संख्या निर्दिष्ट करता है।
निम्न PHP कोड "webdictionary.txt" फ़ाइल को अंत तक पढ़ता है:
fread($myfile,filesize("webdictionary.txt"));
PHP बंद फ़ाइल - fclose ()
इस fclose()
फंक्शन का प्रयोग ओपन फाइल को बंद करने के लिए किया जाता है।
सभी फाइलों को समाप्त करने के बाद उन्हें बंद करना एक अच्छा प्रोग्रामिंग अभ्यास है। आप नहीं चाहते कि आपके सर्वर पर संसाधन लेने वाली एक खुली फ़ाइल चल रही हो!
फ़ाइल के नाम की fclose()
आवश्यकता है (या एक चर जो फ़ाइल नाम रखता है) जिसे हम बंद करना चाहते हैं:
<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>
PHP सिंगल लाइन पढ़ें - fgets ()
फ़ंक्शन का fgets()
उपयोग किसी फ़ाइल से एक पंक्ति को पढ़ने के लिए किया जाता है।
नीचे दिया गया उदाहरण "webdictionary.txt" फ़ाइल की पहली पंक्ति को आउटपुट करता है:
उदाहरण
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
नोट: फ़ंक्शन को कॉल करने के बाद fgets()
, फ़ाइल पॉइंटर अगली पंक्ति में चला गया है।
पीएचपी चेक एंड-ऑफ-फाइल - फीफ ()
फ़ंक्शन जांचता है कि feof()
"एंड-ऑफ-फाइल" (ईओएफ) तक पहुंच गया है या नहीं।
feof()
फ़ंक्शन अज्ञात लंबाई के डेटा के माध्यम से लूपिंग के लिए उपयोगी है ।
नीचे दिया गया उदाहरण "webdictionary.txt" फ़ाइल को पंक्ति दर पंक्ति पढ़ता है, जब तक कि फ़ाइल का अंत नहीं हो जाता:
उदाहरण
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>
PHP एकल वर्ण पढ़ें - fgetc ()
फ़ंक्शन का fgetc()
उपयोग किसी फ़ाइल से एकल वर्ण को पढ़ने के लिए किया जाता है।
नीचे दिया गया उदाहरण "webdictionary.txt" फ़ाइल वर्ण को वर्ण के अनुसार पढ़ता है, जब तक कि फ़ाइल का अंत नहीं हो जाता:
उदाहरण
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?>
नोट: फ़ंक्शन को कॉल करने के बाद fgetc()
, फ़ाइल पॉइंटर अगले वर्ण पर चला जाता है।
पूर्ण PHP फाइलसिस्टम संदर्भ
फाइलसिस्टम फंक्शन के संपूर्ण संदर्भ के लिए, हमारे संपूर्ण पीएचपी फाइलसिस्टम संदर्भ पर जाएं ।