download.php 1.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
<?php
/*

  Copyright (C) 2017 Elphel Inc.

  SPDX-License-Identifier: AGPL-3.0+

  https://www.elphel.com

*/

// https://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly
// * solution for ZipArchive worked only from a command line
// * solution that worked: command line zip

// contstants
$TEXTURE_EXTENSIONS = ['png','jpeg','jpg','tif','tiff','gif'];

// check if file parameter is specified
if (!isset($_GET['file'])) die("-1");

$file = $_GET['file'];

$patterns = ['/(\.)+\//','/^\//'];
$replacements = ['',''];

// check if file exists
if (!is_file($file)) die("-2");
if (substr($file,-4)!=".x3d") die("-3");

$pathinfo = pathinfo($file);
$path = $pathinfo['dirname'];

$tmp = explode("/",$path);

$zipfile = $tmp[1]."_".$tmp[2].".zip";

// alright, there's this file
$contents = file_get_contents($file);

// extract file list
preg_match_all('/url="([^\s]+('.implode('|',$TEXTURE_EXTENSIONS).'))"/i',$contents,$matches);

// make a string
foreach($matches[1] as $v){
  $file .= " $path/$v";
}

$zipped_data = `zip -qj - $file `;
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zipfile.'"');
echo $zipped_data;

?>