hi,
i looking for a class that can handle with winrar and zip files
i have found a lot of resource for zip but none for winrar...
any one know something that work in both of the type files?
thanks
:)

Dani AI

Generated

Short version: on shared hosting you generally cannot support RAR unless the host enables the PHP rar extension or allows calling the unrar/rar binaries. ZIP is widely available via ZipArchive. is correct about using the command line when permitted, and is right that creating RARs is not an option from PHP; the usual toolchain only supports reading/extracting.

A pragmatic approach is feature-detection with graceful fallback: list ZIP contents everywhere, list RAR only when the capability exists, and otherwise show a friendly message or ask users to upload ZIP instead.

function listArchive($path) {
    $ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
    if ($ext === 'zip' && class_exists('ZipArchive')) {
        $zip = new ZipArchive();
        if ($zip->open($path) === true) {
            for ($i = 0; $i < $zip->numFiles; $i++) {
                $stat = $zip->statIndex($i);
                echo htmlspecialchars($stat['name']).' ('.$stat['size'].' bytes)'."\n";
            }
            $zip->close();
            return true;
        }
    }
    if ($ext === 'rar' && class_exists('RarArchive')) {
        $rar = RarArchive::open($path);
        if ($rar) {
            foreach ($rar->getEntries() as $entry) {
                echo htmlspecialchars($entry->getName()).' ('.$entry->getUnpackedSize().' bytes)'."\n";
            }
            $rar->close();
            return true;
        }
    }
    return false; // not supported or failed to open
}

Tips that help on shared hosts:

  • Ask your host if they can enable the PHP rar extension; if not, treat RAR as unsupported and clearly tell users to upload ZIP or GZIP instead.
  • Never extract to the filesystem just to list contents. For previews, read small text entries in memory only, enforce size limits, and time out long operations.
  • Validate archives defensively: check MIME with finfo, cap total entries and uncompressed size, and reject nested archives to avoid zip-bomb issues.

If you later move off shared hosting, you can list RAR via unrar lb or extract via unrar x, but that requires exec permissions and careful sandboxing.

Recommended Answers

All 5 Replies

hi,
thanks for your help but that class return this error:
Fatal error: Class 'RarArchive' not found i think that is not installed on my shared server and the possibl. for install is null
do you know a alternative?

thanks for your help

Don't know of one. Perhaps you can write one. It is possible to call rar through the command line.

You do realize that though you can open and read FROM .rar files, you can't actually create them? WinRar's main site.

If you're looking to open user-uploaded rar and zip files (the thought which scares the living hell out of me) then you will probably want to just stick with zip only, or maybe add gzip.

Hope this helps.

Don't know of one. Perhaps you can write one. It is possible to call rar through the command line.

i using a shared server i think i can´t do that in that kind os server

You do realize that though you can open and read FROM .rar files, you can't actually create them? WinRar's main site.

If you're looking to open user-uploaded rar and zip files (the thought which scares the living hell out of me) then you will probably want to just stick with zip only, or maybe add gzip.

Hope this helps.

yes i know, i don´t want open the files uploaded by the user i want that the script can open and see the content for the users can find the content without downloading the files, this is already working for zip files but some files is compressed in rar format...

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.