0

My ics creator fails when it comes to download the created file. I've tried the following:

function createICS($start, $end, $type, $hrStart, $hrEnd){
$filename = $hrStart."-".$hrEnd." ".$type.".ics";
$string=" 
BEGIN:VCALENDAR 
VERSION:2.0 
PRODID:PHP 
METHOD:REQUEST 
BEGIN:VEVENT
DTSTART:".$start." 
DTEND:".$end."
DESCRIPTION:".$type." 
SUMMARY:".$type." vom ".$hrStart." bis zum  ".$hrEnd."
UID:1
SEQUENCE:0 
DTSTAMP:".date('Ymd')."T".date('His')." 
END:VEVENT
END:VCALENDAR 
"; 
/*!$handle = fopen($filename, 'W');
fwrite($handle, $string);
fclose($handle)*/

/*header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize($filename).";");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/octet-stream; "); 
header("Content-Transfer-Encoding: binary");*/

header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\"");
header("Content-Type: application/force-download");
header("Content-Length: " . filesize($filename));
header("Connection: close");

echo $string;

}

But I get just this response instead of the .ics file: enter image description here

What am I doing wrong? Can someone help me?

Thanks

Yanick

Yanick Schraner
  • 195
  • 3
  • 15
  • 1
    The headers you used are probably getting overridden by the PHP warning about filesize(). You also probably want `strlen($string)` instead of `filesize($filename)` because $filename isn't a real file. – Brian Adams Apr 08 '14 at 18:48
  • well after replace `filesize($filename)` with `strlen($string)` there is no longer an error, but the string got put out like an normal echo... Maybe it's because the icalcreator.php is called by an jQuery post? `$.ajax({ type: "POST", url: "iCal.php", data: { "startDate": multiDateArray[i], "endDate": multiDateArray[i+1], "type": multiDateArray[i+2], "hrStartDate": multiDateArray[i+3], "hrEndDate": multiDateArray[i+4] } });` – Yanick Schraner Apr 08 '14 at 18:58
  • 1
    Ah yes you won't be able to download the file that way. If you don't mind using GET instead of POST, you can set window.location to a URL that will call your function instead. If you want to use POST, this answer may help http://stackoverflow.com/a/9970672/1636812 – Brian Adams Apr 08 '14 at 19:06
  • Thanks for the cool link, worked perfectly!! – Yanick Schraner Apr 08 '14 at 21:17

1 Answers1

0

As @Brian Adams said, relplacing filesize($filename) with strlen($string)solved the problem.

Yanick Schraner
  • 195
  • 3
  • 15