1

I am currently working with jQuery FullCalendar plugin and I just made a function which exports my events into a Google Calendar format. The problem I encounter is that when I download it, I have two extra spaces at the beginning of the file. Unfortunatly, the import fails due to these spaces.

Here's how I create the file and generate the download :

    $ical = "BEGIN:VCALENDAR
            VERSION:2.0";
    foreach($events as $e){
            $ical .= "\nBEGIN:VEVENT
            UID:" . md5(uniqid(mt_rand(), true)) . "@example.test
            DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z
            DTSTART: ". $this->formatDate($e['E_dateStart']) ."
            DTEND:". $this->formatDate($e['E_dateEnd']) ."
            SUMMARY:". $e['E_description'] ."
            END:VEVENT";
        }
    $ical .= "\nEND:VCALENDAR";
    $ical = str_replace("\t", "", $ical);
    $ical = str_replace(" ", "", $ical);

    header('Content-type: text/calendar; charset=utf-8');
    header('Content-Disposition: attachment; filename=calendar.ics');
    echo "$ical";
    exit;

And then the downloaded file looks like this :

    BEGIN:VCALENDAR
  VERSION:2.0
  BEGIN:VEVENT
  UID:3a24a50a6b9af94c0665c6528f9e38aa@example.test
  DTSTAMP:20150330T083230Z
  DTSTART:20150329T000000Z
  DTEND:20150327T001200Z
  SUMMARY:test
  END:VEVENT
  END:VCALENDAR

I have two spaces just before BEGIN VCALENDAR and I can't figure out where it comes from. I'm guessing it is caused by PHP echo but I'm not sure and I don't know how I could do it differently.

MrPanda
  • 99
  • 1
  • 9
  • Does it look OK if you just do `echo $ical` instead of `echo "$ical"`? Alternatively, you can try to run the `$ical` variable through the [trim()](http://php.net/manual/en/function.trim.php) function – kalatabe Mar 30 '15 at 08:50
  • What happens if you comment out the header lines? – Peter Bowers Mar 30 '15 at 08:52
  • If you had errors enabled, you should see a messag eabout "headers already sent" which should tell you where the prior output occurred – Mark Baker Mar 30 '15 at 09:12
  • @KaloyanDoichinov When I remove quotes it doesn't change anything. And same thing with trim. I already tried it. PeterBowers When I comment headers it just echoes my variable with no extra spaces and displays it in the browser but it doesn't generate the download. And that's where it is weird, on the browser there's no spaces... – MrPanda Mar 30 '15 at 09:15
  • 1
    I just tested your code on 2 different machines and it prints out just fine, no extra spaces or anything. It has to be something that happens before this point, as others have advised, check for unwanted output prior to generating the calendar file. – kalatabe Mar 30 '15 at 09:24
  • Indeed, I tried to generate the download on another page and it prints out fine. I will look up for what causes this problem ASAP and post my solution. Thank you for your answers, now I'm sure the problem isn't from my function or whatsoever. – MrPanda Mar 30 '15 at 14:23

3 Answers3

6

I came with the same problem and my friend I found a quick solution for this.

You have to just add a single line before your code.

ob_clean();

$ical = "BEGIN:VCALENDAR
         VERSION:2.0";
Dovydas Šopa
  • 2,282
  • 8
  • 26
  • 34
Nirav Thakar
  • 166
  • 1
  • 10
1

I found out where it came from. If searched into every file I called in my controller and I had extra spaces in some managers after the PHP closing tag.
So if this problem occurs to someone else, just check your managers or whatever you call/initialize in your controller and check the end of the file and delete extra spaces.

MrPanda
  • 99
  • 1
  • 9
0

trim() is your friend. :) If the space still shows when you echo trim($ical), you must have trailing spaces somewhere in your file preceding the code above, possibly before your PHP opening tag.

Markus AO
  • 4,771
  • 2
  • 18
  • 29