comprimere directory in formato zip

luigi777

Utente Attivo
14 Feb 2008
1.086
1
38
42
Massa, Italy
Salve, ho questo codice che funziona a meta perché mi dice che non trova:
<br />
<b>Warning</b>: chdir(): No such file or directory (errno 2) in <b>/web/htdocs/xxxx/home/zip_folders.php</b> on line <b>20</b><br />

e il codice è questo:
PHP:
<?php
    // WARNING
    // This code should NOT be used as is. It is vulnerable to path traversal. https://www.owasp.org/index.php/Path_Traversal
    // You should sanitize $_GET['directtozip']
    // For tips to get started see http://stackoverflow.com/questions/4205141/preventing-directory-traversal-in-php-but-allowing-paths
 
    //Get the directory to zip
    $filename_no_ext= $_GET['directtozip'];
 
    // we deliver a zip file
    header("Content-Type: archive/zip");
 
    // filename for the browser to save the zip file
    header("Content-Disposition: attachment; filename=$filename_no_ext".".zip");
 
    // get a tmp name for the .zip
    $tmp_zip = tempnam ("tmp", "tempname") . ".zip";
 
    //change directory so the zip file doesnt have a tree structure in it.
    chdir('/'.$_GET['directtozip']);
   
    // zip the stuff (dir and all in there) into the tmp_zip file
    exec('zip '.$tmp_zip.' *');
   
    // calc the length of the zip. it is needed for the progress bar of the browser
    $filesize = filesize($tmp_zip);
    header("Content-Length: $filesize");
 
    // deliver the zip file
    $fp = fopen("$tmp_zip","r");
    echo fpassthru($fp);
 
    // clean up the tmp zip file
    unlink($tmp_zip);
?>

idee? cosa devo mettere al chdir?

grazie mille. e buone feste.
 
ok, ho modificato il codice e ora funziona, l'unico che mi fa cosi:
cartella.zip da 7.6mb e dentro c'è un file che si chiama z senza estensione e in tutto e di 20 mb e dove c'è veramente proprio tutto il sito compresso.
solo che in quello che mi fa aprire non comprime le sotto directory etc.. e invece il file dentro cartella.zip nominato z senza nulla c'è tutto .
come mai?
PHP:
<?php
	// http://www.travisberry.com/2010/09/use-php-to-zip-folders-for-download/   // questo il sito dove lo preso.
    //Get the directory to zip
    $filename_no_ext= $_GET['dir'];
 
    // we deliver a zip file
    header("Content-Type: archive/zip");
 
    // filename for the browser to save the zip file
    header("Content-Disposition: attachment; filename=$filename_no_ext".".zip");
 
    // get a tmp name for the .zip
    $tmp_zip = tempnam ("tmp", "tempname") . ".zip";
 
    //change directory so the zip file doesnt have a tree structure in it.
    chdir('/web/htdocs/www.nomedelsito.it/home/'.$_GET['dir']);
   
    // zip the stuff (dir and all in there) into the tmp_zip file
    exec('zip '.$tmp_zip.' *');
   
    // calc the length of the zip. it is needed for the progress bar of the browser
    $filesize = filesize($tmp_zip);
    header("Content-Length: $filesize");
 
    // deliver the zip file
    $fp = fopen("$tmp_zip","r");
    echo fpassthru($fp);
 
    // clean up the tmp zip file
    unlink($tmp_zip);
?>
 

Discussioni simili