Advertisement
VanGans

Backup File Directory

Sep 7th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.30 KB | None | 0 0
  1. <?php
  2.     // WARNING
  3.     // This code should NOT be used as is. It is vulnerable to path traversal. https://www.owasp.org/index.php/Path_Traversal
  4.     // You should sanitize $_GET['directtozip']
  5.     // For tips to get started see http://stackoverflow.com/questions/4205141/preventing-directory-traversal-in-php-but-allowing-paths
  6.     // Coded By XZ-Sec
  7.     // Backup Your Files
  8.      
  9.     //Get the directory to zip
  10.     $filename_no_ext= $_GET['directtozip'];
  11.  
  12.     // we deliver a zip file
  13.     header("Content-Type: archive/zip");
  14.  
  15.     // filename for the browser to save the zip file
  16.     header("Content-Disposition: attachment; filename=$filename_no_ext".".zip");
  17.  
  18.     // get a tmp name for the .zip
  19.     $tmp_zip = tempnam ("tmp", "tempname") . ".zip";
  20.  
  21.     //change directory so the zip file doesnt have a tree structure in it.
  22.     chdir('user_uploads/'.$_GET['directtozip']);
  23.  
  24.    
  25.     // zip the stuff (dir and all in there) into the tmp_zip file
  26.     exec('zip '.$tmp_zip.' *');
  27.    
  28.     // calc the length of the zip. it is needed for the progress bar of the browser
  29.     $filesize = filesize($tmp_zip);
  30.     header("Content-Length: $filesize");
  31.  
  32.     // deliver the zip file
  33.     $fp = fopen("$tmp_zip","r");
  34.     echo fpassthru($fp);
  35.  
  36.     // clean up the tmp zip file
  37.     unlink($tmp_zip);
  38. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement