Hi to everyone,
I am a begginer on php so maybe this question will appear to simple to someone.
My issue is that I want to output a list with all .zip files that are in my site directory.
Also for each file I want some details for exemple:

Filename: Simple.zip
Size: 3.2Mb
Created Date: 11/11/2013
File Path: ....

Could someone help me with my problem?!
Please consider that I am a beginner and some php functions that you may suggest to me may not be very usefully in my case :(

Dani AI

Generated

This thread asked for a simple listing of .zip files with filename, size, created date and path. pointed to iterator- and file-info approaches and confirmed those solved the immediate problem. The snippet below takes a complementary route: it uses PHP's ZipArchive to read archive-level metadata (number of entries, total compressed/uncompressed size) without extracting files, and uses a plain directory scan to find .zip files so it stays approachable for beginners.

<?php
$dir = __DIR__ . '/zips';
$files = scandir($dir);

function human_size($bytes) {
    $units = array('B','KB','MB','GB','TB');
    for ($i=0; $bytes >= 1024 && $i < count($units)-1; $i++) $bytes /= 1024;
    return round($bytes, 2) . ' ' . $units[$i];
}

foreach ($files as $f) {
    if ($f === '.' || $f === '..') continue;
    $path = $dir . DIRECTORY_SEPARATOR . $f;
    if (!is_file($path)) continue;
    if (strtolower(pathinfo($f, PATHINFO_EXTENSION)) !== 'zip') continue;

    $za = new ZipArchive;
    $num = 0; $totUn = $totComp = 0;
    if ($za->open($path) === true) {
        $num = $za->numFiles;
        for ($i = 0; $i < $num; $i++) {
            $st = $za->statIndex($i);
            $totUn   += $st['size'];
            $totComp += $st['comp_size'];
        }
        $za->close();
    }

    echo "Filename: {$f}\n";
    echo "Size: " . human_size(filesize($path)) . "\n";
    echo "Files inside: {$num}\n";
    echo "Total uncompressed: " . human_size($totUn) . "\n";
    echo "Total compressed: " . human_size($totComp) . "\n";
    echo "Path: " . realpath($path) . "\n\n";
}

Notes and cautions: filesystem “created” timestamps vary by OS (on many Unix systems the inode change time is not the original creation time). For the archive’s internal timestamps prefer the per-entry mtime returned by ZipArchive if that represents what’s needed. Avoid opening or extracting untrusted uploads, check size/MIME before processing, limit how many files are scanned at once, and cache results for directories scanned frequently. For recursive scanning, the iterator-style approach mentioned earlier in the thread is a good next step once the basic listing works.

Recommended Answers

All 2 Replies

Thank you man!
The links that you suggested to me solved my issue. Big up for you (y)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.