PHP - 直接I/O dio_close() 函式



PHP 直接 I/O dio_close() 函式用於關閉使用 dio_open() 函式最初開啟的檔案描述符。這是直接 I/O 例程提供的低階檔案 I/O 操作介面的一部分。

語法

以下是 PHP 直接 I/O dio_close() 函式的語法:

void dio_close(resource $fd);

引數

此函式接受 $fd 引數,它是必須關閉的檔案描述符資源。它由 dio_open() 函式給出。

返回值

dio_close() 函式不返回任何值。

PHP 版本

dio_close() 函式首次引入到核心 PHP 4.2.0 中,並且在 PHP 5.1.0 中繼續輕鬆執行。

示例 1

首先,我們將向您展示 PHP 直接 I/O dio_close() 函式關閉使用 dio_open() 函式開啟的檔案的基本示例。

<?php
   /// Open the file
   $fd = dio_open('/PHP/PhpProjects/myfile.txt', O_RDONLY);
   
   // operations to perform

   // Close the file
   dio_close($fd);
   
   echo "File has been closed!!!";
?>

輸出

以上程式碼將產生類似以下的結果:

File has been closed!!!

示例 2

在下面的 PHP 程式碼中,我們將嘗試使用 dio_close() 函式在使用 dio_write() 函式執行寫入操作後關閉已開啟的檔案。

<?php
   // Open the file for writing
   $fd = dio_open('/PHP/PhpProjects/myfile.txt', O_WRONLY | O_CREAT);
   
   // Write data to the file
   dio_write($fd, "Hello, World!");
   
   
   echo "Message is written successfully!!\n";
   
   // Close the file
   dio_close($fd);
   echo "File is closed after performing operation!";
?> 

輸出

執行以上程式後,它將生成以下輸出:

Message is written successfully!!
File is closed after performing operation!

示例 3

現在,以下程式碼在使用 dio_close() 函式時處理錯誤,並在發生錯誤時列印錯誤訊息。

<?php
   // Open the file
   $fd = dio_open('/PHP/PhpProjects/myfile.txt', O_RDONLY);
   
   if ($fd === false) {
       die('Failed to open file');
   }
   
   // Perform file operations here
   
   // Close the file
   if (dio_close($fd) === false) {
       echo 'Failed to close file';
   } else {
       echo 'File closed successfully';
   }
   
?> 

輸出

這將建立以下輸出:

File closed successfully

示例 4

在以下示例中,我們使用 dio_close() 函式獲取具有重定向次數的主 IP 地址。

<?php
   // Open the binary file
   $fd = dio_open('/PHP/PhpProjects/encrypted_file.bin', O_RDONLY);
   
   // Read data from the file
   $data = dio_read($fd, 1024);
   
   
   // print the data
   echo "Binary Data is as follows: \n";
   echo $data;
   
   
   // Close the file
   dio_close($fd);

   echo "\nFile is closed successfully!!"
?> 

輸出

執行以上程式時,它將產生以下輸出:

Binary Data is as follows:
M/]'w???p??????4W?ʓ????????S?q~?????e;Ղ??vҶM?&;3??r|?/(xd??]???]?Ї?sL???\e-.?i#j??I_d`z)?p?u?o??i?	:??ڸ????hڛ?Z????PTł???#?i?[?????o????z?W?? y?p????I???̶on?˾?s?????|s???Mq#?c??k?|G?īv???????C*X???%/??B??
File is closed successfully!!
php_function_reference.htm
廣告