downloadFile.php 1.65 KB
<?php


 function prepend($string, $orig_filename) {
  $context = stream_context_create();
  $orig_file = fopen($orig_filename, 'r', 1, $context);

  $temp_filename = tempnam(sys_get_temp_dir(), 'php_prepend_');
  file_put_contents($temp_filename, $string);
  file_put_contents($temp_filename, $orig_file, FILE_APPEND);

  fclose($orig_file);
  return $temp_filename;
}

 $file=$_GET['path'];
 //echo $file;
 $dir='/home/pi/Desktop/vAlert/rec/';
 $fileArray=explode('/',$file);
 //print_r($fileArray);
 $filename=$fileArray[count($fileArray)-1];
 if(substr($filename,0,4)=="DIN2")
 {  $string[0]="DIN Report of ".substr($filename,3,4)."-".substr($filename,7,2)."-".substr($filename,9,2)."\n";
    $string[1]="Hour:Minutes Seconds,Warning Level,Din Relay Register,Z-axis Peak,Z-axis: Frequency,Z-axis time in seconds,X-axis:Peak,X-axis:Frequency, X-axis time in seconds,Y-axis:Peak,Y-axis:Frequency,Y-axis time in seconds,Vector-axis Peak, Vector-axis:Frequency, Vector-axis time in seconds\n";
    $tmp=file($file);
    $myfileArray=array_merge($string,$tmp);
    $mytempFile=$dir.'download.csv';
    $fp=fopen($mytempFile,'w') or die("Cannot write  file ");
    foreach ($myfileArray as $key => $value) {
    	fwrite($fp,$value);
    }
    header("Content-type:application/pdf");
    header('Content-Disposition: attachment; filename=' . $filename);
    readfile( $mytempFile);
   unlink($mytempFile);
 }
 else{

 	//echo $filename;
 	header("Content-type:application/pdf");
    header('Content-Disposition: attachment; filename=' . $filename);
    readfile( $file);
    //unlink($tmp);
 //echo '<script> window.history.back();</script>';
 }

?>