2010-09-30 16 views
9

C'è un modo per comprimere/archiviare una cartella nel server usando lo script php in .zip o .rar o in qualsiasi altro formato compresso, così che su richiesta potremmo archiviare la cartella e poi dare il link di downloadcartella compressa/archivio usando lo script php

Grazie in anticipo

risposta

14

Ecco un esempio:

<?php 

// Adding files to a .zip file, no zip file exists it creates a new ZIP file 

// increase script timeout value 
ini_set('max_execution_time', 5000); 

// create object 
$zip = new ZipArchive(); 

// open archive 
if ($zip->open('my-archive.zip', ZIPARCHIVE::CREATE) !== TRUE) { 
    die ("Could not open archive"); 
} 

// initialize an iterator 
// pass it the directory to be processed 
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("themes/")); 

// iterate over the directory 
// add each file found to the archive 
foreach ($iterator as $key=>$value) { 
    $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key"); 
} 

// close and save archive 
$zip->close(); 
echo "Archive created successfully."; 
?> 
+1

codice grande ... come posso modificare questo in ZIP una cartella nidificata escludendo tutte le cartelle che portano ad esso? la mia cartella desiderata è disponibile in "/var/www/vhosts/mysite.com/dev/wp-content/themes/mytheme/". quando eseguo questo script ottengo una cartella iniziale di var poi www vhosts then mysite.com ecc. cosa mi manca? –

+0

Ho capito bene che il codice itera anche attraverso "." e ".." le directory all'interno di "temi /"? Sembra che questo causi problemi quando tenti di decomprimere l'archivio. – Tamara

+0

questo codice causa problemi durante la decompressione del file. –

3

Attenzione di un possibile problema nell'esempio di Adnan: Se il myarchive.zip bersaglio è all'interno della cartella sorgente, allora avete bisogno di escluderlo nel ciclo, o per eseguire l'iteratore prima di creare il file di archivio (se non esiste già). Ecco uno script revisionato che utilizza l'ultima opzione e aggiunge alcune vere caratteristiche di configurazione. Questo non dovrebbe essere usato per aggiungere a un archivio esistente.

<?php 
// Config Vars 

$sourcefolder = "./"   ; // Default: "./" 
$zipfilename = "myarchive.zip"; // Default: "myarchive.zip" 
$timeout  = 5000   ; // Default: 5000 

// instantate an iterator (before creating the zip archive, just 
// in case the zip file is created inside the source folder) 
// and traverse the directory to get the file list. 
$dirlist = new RecursiveDirectoryIterator($sourcefolder); 
$filelist = new RecursiveIteratorIterator($dirlist); 

// set script timeout value 
ini_set('max_execution_time', $timeout); 

// instantate object 
$zip = new ZipArchive(); 

// create and open the archive 
if ($zip->open("$zipfilename", ZipArchive::CREATE) !== TRUE) { 
    die ("Could not open archive"); 
} 

// add each file in the file list to the archive 
foreach ($filelist as $key=>$value) { 
    $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key"); 
} 

// close the archive 
$zip->close(); 
echo "Archive ". $zipfilename . " created successfully."; 

// And provide download link ?> 
<a href="http:<?php echo $zipfilename;?>" target="_blank"> 
Download <?php echo $zipfilename?></a> 
+0

Non decomprime. mostra errore su unzip. –

Problemi correlati