當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP zip_entry_close()用法及代碼示例


zip_entry_close()函數是PHP中的內置函數,用於關閉由zip_entry_open()函數打開的zip存檔。 zip_entry_close()導致流關閉,並且與相應Zip存檔條目的連接可能斷開,該條目可能是Zip存檔中的文件或目錄。必須關閉的zip條目資源作為參數發送到zip_entry_close()函數。

用法:

 bool zip_entry_close ( $zip_entry )

參數:zip_entry_close()函數接受單個參數$zip_entry。它是必填參數,用於指定zip條目資源。


返回值:成功返回true,失敗返回False。

錯誤和異常:

  • 必須先使用PHP zip_entry_open()函數打開要關閉的zip條目檔案,否則PHP zip_entry_close()函數將產生PHP警告。
  • 如果zip存檔無效,則zip_entry_close()函數將返回ER_OPEN錯誤。
  • 如果zip存檔為空,則zip_entry_close()函數返回ER_NOZIP錯誤。

Suppose a zip file article.zip contains the following file:
content.xlsx

以下示例程序旨在說明PHP中的zip_entry_close()函數:

程序1:

<?php 
  
// Opening a zip archive 
$zip_handle = zip_open("C:/xampp/htdocs/article.zip"); 
$zip_entry = zip_read($zip_handle); 
   
// Opening a zip entry archive  
zip_entry_open($zip_handle, $zip_entry, "rb"); 
$file = zip_entry_name($zip_entry); 
   
// Closing a zip entry archive  
$flag = zip_entry_close($zip_entry); 
  
if ($flag == true)  
    echo("Zip Entry Archive: " . $file . " has been closed successfully. "); 
else
    echo("Zip Entry Archive: " . $file . " cannot be closed."); 
   
zip_close($zip_handle); 
?>

輸出:

Zip Entry Archive: article/content.xlsx has been closed successfully.

Suppose a zip file article.zip contains the following files:
content.xlsx
gfg.pdf
image.jpeg

程序2:

<?php 
  
// Opening a zip archive 
$zip_handle = zip_open("C:/xampp/htdocs/article.zip"); 
  
if(is_resource($zip_handle))  
{  
    while($zip_entry = zip_read($zip_handle))  
    {  
  
        // Opening a zip archive entry 
        $file = zip_entry_open($zip_handle, $zip_entry, "rb"); 
        $file_name = zip_entry_name($zip_entry); 
          
        if ($file == true)  
        {  
            echo("Zip Entry Archive: " . $file_name .  
                  " has been opened successfully." . "<br>"); 
  
            // Closing a zip archive entry 
            $flag = zip_entry_close($zip_entry); 
              
            if ($flag == true)  
                echo("Zip Entry Archive: " . $file_name . 
                  " has been closed successfully." . "<br>"); 
            else
                echo("Zip Entry Archive: " . $file_name . 
                              " cannot be closed." . "<br>"); 
        }  
        else
            echo("Zip Entry Cannot be opened.");  
    }  
  
    // Closing a zip archive 
    zip_close($zip_handle); 
} 
else
    echo("Failed to Open" . $zip_handle ); 
?>

輸出:

Zip Entry Archive: article/content.xlsx has been opened successfully.
Zip Entry Archive: article/content.xlsx has been closed successfully.
Zip Entry Archive: article/gfg.pdf has been opened successfully.
Zip Entry Archive: article/gfg.pdf has been closed successfully.
Zip Entry Archive: article/image.jpeg has been opened successfully.
Zip Entry Archive: article/image.jpeg has been closed successfully.

參考: http://php.net/manual/en/function.zip-entry-close.php



相關用法


注:本文由純淨天空篩選整理自Shubrodeep Banerjee大神的英文原創作品 PHP | zip_entry_close() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。