Beispiele

See also the examples under rar:// wrapper.

Beispiel #1 On-the-fly decompression

<?php

if (!array_key_exists("i"$_GET) || !is_numeric($_GET["i"]))
    die(
"Index unspecified or non-numeric");
$index = (int) $_GET["i"];
    
$arch RarArchive::open("example.rar");
if (
$arch === FALSE)
    die(
"Cannot open example.rar");

$entries $arch->getEntries();
if (
$entries === FALSE)
    die(
"Cannot retrieve entries");

if (!
array_key_exists($index$entries))
    die(
"No such index: $index");

$orfilename $entries[$index]->getName(); //UTF-8 encoded

$filesize $entries[$index]->getUnpackedSize();

/* you could check HTTP_IF_MODIFIED_SINCE here and compare with
 * $entries[$index]->getFileTime(). You could also send a
 * "Last modified" header */

$fp $entries[$index]->getStream();
if (
$fp === FALSE)
    die(
"Cannot open file with index $index insided the archive.");

$arch->close(); //no longer needed; stream is independent

function detectUserAgent() {
    if (!
array_key_exists('HTTP_USER_AGENT'$_SERVER))
        return 
"Other";
    
    
$uas $_SERVER['HTTP_USER_AGENT'];
    if (
preg_match("@Opera/@"$uas))
        return 
"Opera";
    if (
preg_match("@Firefox/@"$uas))
        return 
"Firefox";
    if (
preg_match("@Chrome/@"$uas))
        return 
"Chrome";
    if (
preg_match("@MSIE ([0-9.]+);@"$uas$matches)) {
        if (((float)
$matches[1]) >= 7.0)
            return 
"IE";
    }
    
    return 
"Other";
}

/*
 * We have 3 options:
 * - For FF and Opera, which support RFC 2231, use that format.
 * - For IE and Chrome, use attwithfnrawpctenclong
 *   (http://greenbytes.de/tech/tc2231/#attwithfnrawpctenclong)
 * - For the others, convert to ISO-8859-1, if possible
 */
$formatRFC2231 'Content-Disposition: attachment; filename*=UTF-8\'\'%s';
$formatDef 'Content-Disposition: attachment; filename="%s"';

switch (
detectUserAgent()) {
    case 
"Opera":
    case 
"Firefox":
        
$orfilename rawurlencode($orfilename);
        
$format $formatRFC2231;
        break;

    case 
"IE":
    case 
"Chrome":
        
$orfilename rawurlencode($orfilename);
        
$format $formatDef;
        break;
    default:
        if (
function_exists('iconv'))
            
$orfilename =
                @
iconv("UTF-8""ISO-8859-1//TRANSLIT"$orfilename);
        
$format $formatDef;
}

header(sprintf($format$orfilename));
//cannot send error messages from now on (headers already sent)

//replace by real content type, perhaps infering from the file extension
$contentType "application/octet-stream";
header("Content-Type: $contentType");

header("Content-Transfer-Encoding: binary");

header("Content-Length: $filesize");

if (
$_SERVER['REQUEST_METHOD'] == "HEAD")
    die();
    
while (!
feof($fp)) {
    
$s = @fread($fp8192);
    if (
$s === false)
        break; 
//useless to send error messages
  
    
echo $s;
}
?>

This example opens a RAR file and presents the requested file inside the RAR archive for download to the client.

Beispiel #2 RAR extension filesystem extraction example

<?php

$rar_file 
rar_open('example.rar') or die("Can't open Rar archive");

$entries rar_list($rar_file);

foreach (
$entries as $entry) {
    echo 
'Filename: ' $entry->getName() . "\n";
    echo 
'Packed size: ' $entry->getPackedSize() . "\n";
    echo 
'Unpacked size: ' $entry->getUnpackedSize() . "\n";

    
$entry->extract('/dir/extract/to/');
}

rar_close($rar_file);

?>

This example opens a RAR file archive and extracts each entry to the specified directory.

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Was genau bedeutet "Vibe Coding"? Ein tiefgehender Blick für Entwickler

In der Welt der Softwareentwicklung gibt es unzählige Wege, wie man an ein Projekt herangeht. Manche schwören auf strikte Planung, andere auf bewährte Algorithmen und wieder andere lassen sich von etwas ganz anderem leiten: ihrem Gefühl. ...

admin

Autor : admin
Kategorie: Software & Web-Development

PHP cURL-Tutorial: Verwendung von cURL zum Durchführen von HTTP-Anfragen

cURL ist eine leistungsstarke PHP-Erweiterung, die es Ihnen ermöglicht, mit verschiedenen Servern über verschiedene Protokolle wie HTTP, HTTPS, FTP und mehr zu kommunizieren. ...

TheMax

Autor : TheMax
Kategorie: PHP-Tutorials

Midjourney Tutorial - Anleitung für Anfänger

Über Midjourney, dem Tool zur Erstellung digitaler Bilder mithilfe von künstlicher Intelligenz, gibt es ein informatives Video mit dem Titel "Midjourney Tutorial auf Deutsch - Anleitung für Anfänger" ...

Mike94

Autor : Mike94
Kategorie: KI Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

Best practices for PHP in 2025?

I’ve been working with PHP for a while, but I’m curious about the community’s thoughts: What do you think are the most important best pract ...

Geschrieben von helen9k am 08.09.2025 03:49:25
Forum: Ankündigungen
How do you keep up with PHP and Database updates?

Hello everyone, PHP and database systems (MySQL, PostgreSQL, etc.) are evolving so quickly. Sometimes it feels hard to stay updated with new vers ...

Geschrieben von helen9k am 06.09.2025 10:35:52
Forum: Ankündigungen
Best Way to Handle Millions of Rows in MySQL – Indexing vs Partitioning?

Hi everyone, I’m working with a MySQL table that already has several million rows, and performance is starting to slow down (queries and joins ...

Geschrieben von helen9k am 06.09.2025 10:31:12
Forum: SQL / Datenbanken
Best Practices for Handling User Login and Session Security in PHP

Hi everyone, I’ve been working on a small PHP project with user login functionality, and I want to make sure I’m following good practices for ...

Geschrieben von helen9k am 06.09.2025 09:39:26
Forum: PHP Developer Forum