简体   繁体   中英

How to download an ics file generated in PHP without using echo / print

I am creating some events on PHP and I'd like to download an.ics file containing these several events to upload them later on my for example, outlook calendar. With "print" or "echo" at the end it works fine but on the platform it is going to be used, I am not allowed to use these two functions. Is there an another way to output this event data?

What I have done so far is,

            $code = "BEGIN:VCALENDAR\n";
            $code .= "VERSION:2.0\n";
            $code .= "METHOD:PUBLISH\n";
            $code .= "BEGIN:VEVENT\n";
            $code .= "DTSTART:" . date("Ymd\THis\Z", strtotime($start_date_time)) . "\n";
            $code .= "DTEND:" . date("Ymd\THis\Z", strtotime($end_date_time)) . "\n";
            $code .= "LOCATION:" . $location . "\n";
            $code .= "TRANSP: OPAQUE\n";
            $code .= "SEQUENCE:0\n";
            $code .= "UID:\nDTSTAMP:" . date("Ymd\THis\Z") . "\n";
            $code .= "SUMMARY:" . $event['summary'] . "\n";
            $code .= "DESCRIPTION:" . $event['description'] . "\n";
            $code .= "PRIORITY:1\n";
            $code .= "CLASS:PUBLIC\n";
            $code .= "BEGIN:VALARM\n";
            $code .= "TRIGGER:-PT10080M\n";
            $code .= "ACTION:DISPLAY\n";
            $code .= "DESCRIPTION:Reminder\n";
            $code .= "End:VALARM\n";
            $code .= "End:VEVENT\n";
            $code .= "End:VCALENDAR\n";

            header('Content-type:text/calendar');
            header("Content-Description: File Download");
            header('Content-Disposition: attachment; filename="events.ics"');
            header("Content-Type: application/force-download");
            header('Connection: close');
            print $code;
    ```

Since the framework builder was handling the outputs all I needed was to assign the part $code to the variable that is being outputted.

All your answers about using print or echo or downloading file were correct if someone needs to do this in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM