diff --git a/tests/php/test_download_modal_remaining_space.php b/tests/php/test_download_modal_remaining_space.php
new file mode 100644
index 0000000000..11915e7cee
--- /dev/null
+++ b/tests/php/test_download_modal_remaining_space.php
@@ -0,0 +1,90 @@
+id = $id; }
+ public function Id() { return $this->id; }
+ public function __get($n) { return $n == 'Id' ? $this->id : null; }
+ public function canView() { return true; }
+ public function DiskSpace() { return $GLOBALS['eventSize']; }
+ }
+}
+
+namespace {
+ $root = isset($argv[1]) ? rtrim($argv[1], '/') : __DIR__.'/../..';
+
+ $exports = sys_get_temp_dir().'/zm_download_remaining_'.getmypid();
+ define('ZM_DIR_EXPORTS', $exports);
+
+ $stubdir = sys_get_temp_dir().'/zm_download_remaining_stub_'.getmypid();
+ @mkdir($stubdir.'/includes', 0700, true);
+ file_put_contents($stubdir.'/includes/Filter.php', " array('5'));
+ ob_start();
+ include $root.'/web/ajax/modals/download.php';
+ echo ob_get_clean();
+ exit(0);
+ }
+
+ $failures = 0;
+ $run = function($case) use ($root) {
+ return (string)shell_exec(escapeshellarg(PHP_BINARY).' '.escapeshellarg(__FILE__).' '.escapeshellarg($root).' '.$case.' 2>&1');
+ };
+
+ $out = $run('over');
+ if (preg_match('/Will remain free: -\d+\.\d\d[kMGT]B/', $out)) {
+ echo "ok - a shortfall is scaled and signed\n";
+ } else {
+ $failures++;
+ preg_match('/Will remain free: [^<\n]*/', $out, $mm);
+ echo "FAIL - a shortfall is not scaled (".($mm[0] ?? 'no line').")\n";
+ }
+
+ $out = $run('under');
+ if (preg_match('/Will remain free: \d+\.\d\d[kMGTP]B/', $out)) {
+ echo "ok - spare space is shown as before\n";
+ } else {
+ $failures++;
+ echo "FAIL - spare space line changed\n";
+ }
+
+ @rmdir($exports);
+ @unlink($stubdir.'/includes/Filter.php');
+ @rmdir($stubdir.'/includes');
+ @rmdir($stubdir);
+ exit($failures ? 1 : 0);
+}
diff --git a/web/ajax/modals/download.php b/web/ajax/modals/download.php
index 2c8c7103db..6c79533c07 100644
--- a/web/ajax/modals/download.php
+++ b/web/ajax/modals/download.php
@@ -126,7 +126,9 @@ if ($exportFileName == 'zmDownload') $exportFileName .= '-'.$connkey;
unset($eid);
$result .= 'Downloading ' . $viewable_count . ' events. Resulting file should be approximately ' . human_filesize($total_size).PHP_EOL;
$result .= '
Free space on disk \''.ZM_DIR_EXPORTS.'\': ' . human_filesize(disk_free_space(ZM_DIR_EXPORTS)).PHP_EOL;
- $result .= '
Will remain free: ' . human_filesize(disk_free_space(ZM_DIR_EXPORTS) - $total_size).PHP_EOL;
+ # human_filesize() does not scale a negative size, so a download larger than the free space showed as bytes.
+ $remaining = disk_free_space(ZM_DIR_EXPORTS) - $total_size;
+ $result .= '
Will remain free: ' . ($remaining < 0 ? '-' : '') . human_filesize(abs($remaining)).PHP_EOL;
if (count($eids) > 1000 and !$filter) {
$result .= 'Warning: Too many recordings specified. Download may fail. Please select fewer recordings';
}