العلميات على الملفات File Open/Read/Close
عمليات الفتح والقراءة والاغلاق
ملاحظة : اذا كانت الوظيفة fopen() غير قادرة على فتح ملف معين عندها ستعود القيمة 0 .
دالة اغلاق للملف ( )Close File - fclose
تستخدم هذه الدالة لاغلاق الملف المفتوح
فحص آخر الملف ( ) feof
يفحص الوظيفة feof نهاية الملف أي في حالة تم قراءة الملف بشكل كامل .تعتبر الوظيفة feof مثالية في استخدام الحلقات عبر البيانات الغير معروفة الطول .
قراءة الملف كلمة بكلمة ( ) fgetc
عمليات الفتح والقراءة والاغلاق
دالة فتح الملف ( ) Open File - fopen
توفر لنا هذه الطريقة الكثير من الخيارات
نفترض ان الملف هو "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
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("webdictionary.txt", "r")
حيث يمثل المعامل الاول اسم الملف المراد فتحه ,والمعامل الثاني R هو هي تحديد حالة الملف ويكون على عدة حالات هي :
مثال1//
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
دالة القراءة ( ) Read File - fread
دالة قراءة الملف المفتوح حيث يمثل المعامل الاول اسم الملف ,والمعامل الثاني الحد الاقصى لقراءة عدد البايتات للملف
fread($myfile,filesize("webdictionary.txt"));
تستخدم هذه الدالة لاغلاق الملف المفتوح
<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>
دالة القراءة لسطر واحد من الملف ( )Single Line - fgets
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
فحص آخر الملف ( ) feof
يفحص الوظيفة feof نهاية الملف أي في حالة تم قراءة الملف بشكل كامل .تعتبر الوظيفة feof مثالية في استخدام الحلقات عبر البيانات الغير معروفة الطول .
ملاحظة : لايمكنك قراءة الملفات المفتوحة من خلال w و a و x .
<?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);
?>
$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);
?>
تستخدم الوظيفة ( )fgetc لقراءة كلمة واحدة من الملف .
ملاحظة : بعد استخدام هذه الوظيفة سينتقل المؤشر للكلمة الثاني .
<?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);
?>
$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);
?>
ليست هناك تعليقات :
إرسال تعليق