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


PHP ZipArchive count()用法及代碼示例


ZipArchive::count() 函數是 PHP 中的內置函數,用於計算 zip 存檔中的文件數量。

用法:

int ZipArchive::count()

參數:該函數不接受任何參數。

返回值:此函數返回 zip 存檔中的文件數。

示例 1:以下代碼演示了 zip 文件的總數。

PHP


<?php
    // Create a new ZipArchive object
    $zip = new ZipArchive;
    // Check for opening the zip file
    if ($zip->open('Geeks.zip')) 
    {            
        // If zip file is open then add an
        // empty directory "GeeksforGeeks"
        if($zip->addEmptyDir('GeeksforGeeks'));
             
        echo 'Total files/directory: ' . $zip->count();
             
        // Close the zip file
        $zip->close();
    }
    // If zip file is not open/exist
    else
    {
        echo 'Failed to open zip file';
    }
?>

輸出:

示例 2:以下代碼顯示了添加代碼中給出的額外 3 個文件後的文件總數。

PHP


<?php
    // Create a new ZipArchive object
    $zip = new ZipArchive;
    // Check for opening the zip file
    if ($zip->open('Geeks.zip', ZipArchive::CREATE)) {
         
        echo 'Total number of files: '
              . $zip->count() . '<br>';
        // Create new txt file and
        // add String to the file
        $zip->addFromString(
            'GFG1.txt', 
            'Welcome to GeeksforGeeks'
        );
        $zip->addFromString(
            'GFG2.txt', 
            'A computer science portal'
        );
        $zip->addFromString(
            'GFG3.txt', 
            'Welcome to GeeksforGeeks'
        );
         
        echo 'Total files after adding new files: '
            . $zip->count();
         
        // Close the zip file
        $zip->close();
    }
    // If zip file is not open/exist
    else {
        echo 'Failed to open zip file';
    }
?>

輸出:

參考: https://www.php.net/manual/en/ziparchive.count.php



相關用法


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