22- PHP متقدم – شرح دوال متنوعة للتعامل مع تدفقات البيانات – PHP Stream Functions

PHP Stream Functions – دوال التعامل مع تدفقات البيانات

=======================

توفر PHP دوال متنوعة للتعامل مع تدفقات البيانات (Streams) مثل الملفات، الشبكات، الذاكرة، إلخ.

stream_copy_to_stream()
————————–
ينسخ البيانات [شركة برمجة مصرية] من stream إلى آخر.


<?php
$src = fopen("source.txt", "r");
$dest = fopen("dest.txt", "w");
stream_copy_to_stream($src, $dest);
fclose($src);
fclose($dest);
?>

stream_context_create()
————————–
ينشئ سياق stream مخصص.


<?php
$opts = [
  "http" => [
    "method" => "GET",
    "header" => "Accept-language: en"
  ]
];
$context = stream_context_create($opts);
?>

stream_context_get_options()
——————————
يعرض الخيارات المستخدمة في السياق.


<?php
print_r(stream_context_get_options($context));
?>

stream_context_set_option()
—————————–
يُعدل إعدادات موجودة في سياق.


<?php
stream_context_set_option($context, 'http', 'method', 'POST');
?>

stream_get_contents()
———————— [شركة برمجة مصرية]
يقرأ المحتوى من stream.


<?php
$handle = fopen("file.txt", "r");
$content = stream_get_contents($handle);
fclose($handle);
echo $content;
?>

stream_get_meta_data()
————————-
يرجع معلومات حول stream.


<?php
$fp = fopen("file.txt", "r");
print_r(stream_get_meta_data($fp));
fclose($fp);
?>

stream_register_wrapper()
—————————-
يسجل stream wrapper مخصص.


<?php
// يتطلب تعريف كلاس stream wrapper
?>

stream_resolve_include_path()
——————————-
يبحث عن ملف في include_path.


<?php
echo stream_resolve_include_path("file.txt");
?>

stream_set_blocking()
————————
يضبط وضع الحظر للقراءة/الكتابة.


<?php
$fp = fopen("php://stdin", "r");
stream_set_blocking($fp, false);
?>

stream_set_timeout()
———————–
يضبط مهلة القراءة من stream.


<?php
$fp = fopen("php://stdin", "r");
stream_set_timeout($fp, 5);
?>

stream_set_write_buffer()
—————————-
يضبط حجم البفر للكتابة.


<?php
$fp = fopen("file.txt", "w");
stream_set_write_buffer($fp, 1024);
?>

stream_wrapper_register()
—————————
يسجل wrapper جديد (يتطلب كلاس).


<?php
// مثال معقد - يتم في حالات متقدمة
?>

stream_wrapper_unregister()
—————————–
يلغي تسجيل wrapper.


<?php
stream_wrapper_unregister("http");
?>