PHP文件下载增加了额外的空间 [英] PHP file download adds extra spaces

查看:65
本文介绍了PHP文件下载增加了额外的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用jQuery FullCalendar插件,并且我刚刚制作了一个函数,可将我的事件导出为Google日历格式. 我遇到的问题是,当我下载它时,文件开头有两个额外的空格.不幸的是,由于这些空格,导入失败.

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

我在BEGIN VCALENDAR之前有两个空格,我不知道它来自哪里.我猜这是由PHP echo引起的,但是我不确定,我也不知道该怎么做.

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.

推荐答案

我遇到了同样的问题,我的朋友也找到了一个快速解决方案.

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";

这篇关于PHP文件下载增加了额外的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆