PHP - FileInfo file() 函式



PHP FileInfo finfo_file() 函式用於返回有關檔案的資訊。它還可以返回檔名引數內容的文字描述,如果發生錯誤則返回 false。

語法

以下是 PHP FileInfo finfo_file() 函式的語法:

string finfo_file ( resource $finfo , string $file_name [, int $options [, resource $context ]] )

引數

以下是 finfo_file() 函式的引數:

  • $finfo - 它是 finfo_open() 返回的 finfo 例項。

  • $file_name - 這是要檢查的檔名。

  • $options - 用於指定一個或多個選項,其預設值為 FILEINFO_NONE。

  • $context - 用於指定由 stream_context_create() 建立的有效上下文資源。

返回值

finfo_file() 函式返回包含檢測到的檔案型別和 MIME 型別的字串,失敗則返回 FALSE。

PHP 版本

finfo_file() 函式首次出現在核心 PHP 5.3.0 中,在 PHP 7 和 PHP 8 中繼續輕鬆執行。

示例 1

以下是 PHP FileInfo finfo_file() 函式的基本示例,用於獲取有關檔案的資訊。

<?php
   // Return MIME type ala mimetype extension
   $finfo = finfo_open(FILEINFO_MIME);  
   foreach(glob("*") as $filename) {
      echo finfo_file($finfo, $filename) . "\n";
   }
   finfo_close($finfo);
?>

輸出

以下是以下程式碼的結果:

text/html; charset=us-ascii
text/plain; charset=us-ascii
text/csv; charset=us-ascii
text/csv; charset=us-ascii
application/zip; charset=binary
text/plain; charset=us-ascii
text/plain; charset=us-ascii

示例 2

在下面的 PHP 程式碼中,finfo_file() 函式接受兩個引數並返回給定檔案的 MIME 型別。

<?php
   // Mention file address here
   $file = '/PHP/PhpProjects/myfile.txt';

   // Create a fileinfo resource
   $finfo = finfo_open(FILEINFO_MIME_TYPE);
   
   // Check if fileinfo resource is valid
   if ($finfo !== false) {
      // Get MIME type
      $info = finfo_file($finfo, $file);
      if ($info !== false) {
         echo "MIME type: $info";
      } else {
         echo "Failed to retrieve MIME type.";
      }
   
      // Close fileinfo resource
      finfo_close($finfo);
   } else {
      echo "Failed to open fileinfo.";
   }
?> 

輸出

這將生成以下輸出:

MIME type: text/plain

示例 3

下面的程式碼使用 finfo_file() 和 $context 屬性檢索遠端檔案的檔案資訊。

<?php
   // Mention file path here
   $file = 'https://tutorialspoint.tw/sample.pdf';

   // Create a fileinfo resource
   $finfo = finfo_open(FILEINFO_MIME_TYPE);
   
   // Check if fileinfo resource is valid
   if ($finfo !== false) {
      // Get file content
      $file_content = file_get_contents($file, false, $context);
   
      // Get MIME type from file content
      $info = finfo_buffer($finfo, $file_content);
       
      if ($info !== false) {
         echo "MIME type of remote file: $info";
      } else {
         echo "Failed to retrieve MIME type of remote file.";
      }
   
      // Close fileinfo resource
      finfo_close($finfo);
   } else {
      echo "Failed to open fileinfo.";
   }
?> 

輸出

這將建立以下輸出:

MIME type of remote file: application/pdf

示例 4

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

<?php
   // Mention file path here
   $file = 'nonexistent-file.txt';

   // Check if file exists
   if (file_exists($file)) {
      // Create a fileinfo resource
      $finfo = finfo_open(FILEINFO_MIME_TYPE);
   
      // Check if fileinfo resource is valid
      if ($finfo !== false) {
         // Get MIME type
         $info = finfo_file($finfo, $file);
         if ($info !== false) {
            echo "File type: $info";
         } else {
            echo "Failed to retrieve file information.";
         }
   
         // Close fileinfo resource
         finfo_close($finfo);
       } else {
         echo "Failed to open fileinfo.";
       }
   } else {
      echo "File '$file' does not exist.";
   }
?> 

輸出

以下是上述程式碼的輸出:

File 'nonexistent-file.txt' does not exist.

總結

finfo_file() 函式是 PHP 中的內建方法,用於檢索檔案資訊。在本節中,我們看到了四個不同的示例,以更好地理解該函式的用法。

php_function_reference.htm
廣告