downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

Fonctions Rar> <Constantes pré-définies
[edit] Last updated: Wed, 19 Jun 2013

view this page in

Exemples

Voir aussi les exemples de la section sur le gestionnaire rar://.

Exemple #1 Décompression à la volée

<?php

if (!array_key_exists("i"$_GET) || !is_numeric($_GET["i"]))
    die(
"Index non spécifié ou non numérique");
$index = (int) $_GET["i"];
    
$arch RarArchive::open("example.rar");
if (
$arch === FALSE)
    die(
"Impossible d'ouvrir example.rar");

$entries $arch->getEntries();
if (
$entries === FALSE)
    die(
"Impossible de récupérer les entrées");

if (!
array_key_exists($index$entries))
    die(
"L'index $index n'existe pas");

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

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

/* Vous pouvez vérifier ici HTTP_IF_MODIFIED_SINCE et le comparer
 * avec $entries[$index]->getFileTime(). Vous pouvez également envoyer
 * un en-tête "Last modified" */

$fp $entries[$index]->getStream();
if (
$fp === FALSE)
    die(
"Impossible d'ouvrir le fichier d'index $index depuis l'archive.");

$arch->close(); //plus nécessaire ; le flux est indépendant

function detectUserAgent() {
    if (!
array_key_exists('HTTP_USER_AGENT'$_SERVER))
        return 
"Autre";
    
    
$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 
"Autre";
}

/*
 * Nous avons 3 options :
 * - Pour FF et Opera, qui supportent RFC 2231, utilisez ce format.
 * - Pour IE et Chrome, utilisez attwithfnrawpctenclong
 *   (http://greenbytes.de/tech/tc2231/#attwithfnrawpctenclong)
 * - Pour les autres, convertissez en ISO-8859-1, si 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));
// Impossible d'envoyer les messages d'erreur à partir d'ici (les en-têtes
// ont déjà été émis)

// Remplacement par le type de contenu réel, pourquoi pas par déduction de l'extension du fichier
$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; 
//utile pour envoyer des messages d'erreur
  
    
echo $s;
}
?>

Cet exemple ouvre l'archive RAR et extrait chacune des entrées dans un dossier spécifié.

Exemple #2 Exemple d'extractions RAR

<?php

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

$entries rar_list($rar_file);

foreach (
$entries as $entry) {
    echo 
'Nom du fichier : ' $entry->getName() . "\n";
    echo 
'Taille de l'archive ' . $entry->getPackedSize() . "\n";
    echo '
Taille après décompression ' . $entry->getUnpackedSize() . "\n";

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

rar_close($rar_file);

?>

Cet exemple ouvre une archive RAR et extrait chaque entrée dans le dossier spécifié.



add a note add a note User Contributed Notes Exemples - [2 notes]
up
0
Nitrogen
2 years ago
A veeery simple function to RAR files, I'm not proud of it.
Since there's no way to create RAR files in PHP (due to licensing, patents or whatever), I'm taking some advantage from the command-line RARing tool that comes with WinRAR (in the WinRAR program files named "rar.exe").

<?php
function RARFiles($Output='output.rar',$Files=array()) {
 
$Data='';
  for(
$i=0;$i<count($Files);$i++)
   
$Data.="\"{$Files[$i]}\" ";
 
exec("rar.exe a \"{$Output}\" {$Data}");
}

$Files=array('file1.ext','file2.ext','file3.ext');
RARFiles('asdf.rar',$Files);
// asdf.rar created.
?>

There's no error checking, so make sure you check that your expected RAR file exists before doing anything with it.
Hopefully one day, PHP will be able to be allowed to create RAR files.
up
0
landavia81 at gmail dot com
4 years ago
for unknown reason.. many programer has problem with php_rar (in windows).. i came up with this ... and it work.

basicly is same as above but only extract only. The problem will come up if the file rar corrupt, exist and soon
_______________________________
<?php
$appRar
="C:/Program Files/WinRAR/unrar.exe"
#this is false..
  
$appRar = '"C:\Program Files\WinRAR\unrar.exe"';
#change this
$opt="e";
$target="1.rar";

$do ="$appRar $opt $target";

exec($do,$aOut);
print_r($aOut);

?>
-------------------------------------------------
For windows user: try using bat file for test.

all user: please read man unrar, above using e because this only extract. But you should notice about other option like "y","x", etc

i type the option here if you too lazy
http://landavia.multiply.com/journal/item/284/option_for_unrar

$aOut are array for the output after you run the script $do

 
show source | credits | sitemap | contact | advertising | mirror sites