Build Tar file from directory in PHP without exec/passthru

前端 未结 4 931
面向向阳花
面向向阳花 2021-01-02 06:19

So I have a client who\'s current host does not allow me to use tar via exec()/passthru()/ect and I need to backup the site periodicly and programmaticly so is there a solut

4条回答
  •  情话喂你
    2021-01-02 07:21

    I need a solution that would work on Azure websites (IIS) and had trouble with creating new files on the server using methods from other answers. The solution that worked for me was to use small TbsZip library for compression, which doesn't require to write file anywhere in the server - it's just returned directly via HTTP.

    This thread is old, but this approach might be a bit more generic and complete answer, so I post the code as alternative:

    // Compress all files in current directory and return via HTTP as a ZIP file
    // by buli, 2013 (http://buli.waw.pl)
    // requires TbsZip library from http://www.tinybutstrong.com
    
    include_once('tbszip.php'); // load the TbsZip library
    $zip = new clsTbsZip(); // instantiate the class
    $zip->CreateNew(); // create a virtual new zip archive
    
    // iterate through files, skipping directories
    $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.'));
    foreach($objects as $name => $object)
    { 
        $n = str_replace("/", "\\", substr($name, 2)); // path format
        $zip->FileAdd($n, $n, TBSZIP_FILE); // add fileto zip archive
    }
    
    $archiveName = "backup_".date('m-d-Y H:i:s').".zip"; // name of the returned file 
    $zip->Flush(TBSZIP_DOWNLOAD, $archiveName); // flush the result as an HTTP download
    

    And here's the whole article on my blog.

提交回复
热议问题