فتح / قراءة / إغلاق ملف PHP
سنعلمك في هذا الفصل كيفية فتح ملف على الخادم وقراءته وإغلاقه.
ملف PHP المفتوح - fopen ()
أفضل طريقة لفتح الملفات هي باستخدام 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()
من ملف مفتوح.
تحتوي المعلمة الأولى 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()
الوظيفة ، انتقل مؤشر الملف إلى السطر التالي.
PHP Check-End-File - feof ()
feof()
تتحقق الوظيفة من الوصول إلى "نهاية الملف" (EOF) .
الوظيفة 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 الكامل
للحصول على مرجع كامل لوظائف نظام الملفات ، انتقل إلى مرجع نظام ملفات PHP الكامل .