Blame view

php/downloadPlot.php 1.83 KB
37e81bff   Benjamin Renard   Direct save of pl...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php

require_once 'config.php';

if (!isset($_POST['sessionId']))
{
	header("HTTP/1.0 400 Bad Request");
	echo json_encode(array("success" => false, "error" => "Unknown session Id"));
	exit;
}
$sessionId = $_POST['sessionId'];

if (!isset($_POST['tabId']))
{
	header("HTTP/1.0 400 Bad Request");
	echo json_encode(array("success" => false, "error" => "Unknown tab Id"));
	exit;
}
$tabId     = $_POST['tabId'];

ebafe59e   Benjamin Renard   Add the possibili...
21
$preview = empty($_POST['preview']) ? FALSE : $_POST['preview'];
37e81bff   Benjamin Renard   Direct save of pl...
22

ebafe59e   Benjamin Renard   Add the possibili...
23
24
25
download_plot($sessionId, $tabId, $preview);

function download_plot($sessionId, $tabId, $preview)
37e81bff   Benjamin Renard   Direct save of pl...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{
	// Must be fresh start
	if( headers_sent() )
	{
		header("HTTP/1.0 400 Bad Request");
		echo json_encode(array("success" => false, "error" => "Headers Sent"));
		return;
	}

	// Required for some browsers
	if(ini_get('zlib.output_compression'))
		ini_set('zlib.output_compression', 'Off');

	//Build file path
ebafe59e   Benjamin Renard   Add the possibili...
40
41
42
43
44
45
	if (!$preview) {
		$fullPath = USERPATH."/".$sessionId."/RES/Plot_/".$tabId.".png";
	}
	else {
		$fullPath = USERPATH."/".$sessionId."/RES/Plot_/".str_replace('plot_','instant',$tabId).".png";
	}
37e81bff   Benjamin Renard   Direct save of pl...
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	// File Exists?
	if( file_exists($fullPath) ){

		// Parse Info / Get Extension
		$fsize = filesize($fullPath);
		$path_parts = pathinfo($fullPath);
		$ctype="image/png";
		
		header("Pragma: public"); // required
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Cache-Control: private",false); // required for certain browsers
		header("Content-Type: $ctype");
		header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" );
		header("Content-Transfer-Encoding: binary");
		header("Content-Length: ".$fsize);
		ob_clean();
		flush();
		readfile( $fullPath );

	} else
	{
		header("HTTP/1.0 400 Bad Request");
		echo json_encode(array("success" => false, "error" => "No existing plot"));
		return;
	}
}

ebafe59e   Benjamin Renard   Add the possibili...
74
?>