编程语言
首页 > 编程语言> > php – 无法在Chrome或Firefox中下载生成的.ics文件

php – 无法在Chrome或Firefox中下载生成的.ics文件

作者:互联网

我通过我的php脚本生成了一个ics文件,它允许我在Safari(6.0.2)中下载.ics文件但在Chrome(23.0.1271.101)和Firefox(17.0.1)中我收到一条错误消息:

Error 6 (net::ERR_FILE_NOT_FOUND): The file or directory could not be found.

.ics文件有效(使用两个单独的源进行检查),我可以在iCal中打开它.我在这个论坛和其他许多论坛上都遵循了脚本和提示.

这是生成.ics文件的代码:

        $tz_sthlm = new DateTimeZone( 'Europe/Stockholm' );
        $tz_utc = new DateTimeZone('UTC');
        $dateEvent = new DateTime( $event->datetime, $tz_sthlm );
        $dateEvent->setTimezone( $tz_utc );

        $filename = str_replace('/', '-', $event->webblink);

        $output = '';

        $output .= 'BEGIN:VCALENDAR' . "\r\n";
        $output .= 'VERSION:2.0' . "\r\n";
        $output .= 'PRODID:-//Medicinska Foreningen Orebro//Biljettbokning//SV-SE' . "\r\n";
        $output .= 'METHOD:PUBLISH' . "\r\n";
        $output .= 'BEGIN:VEVENT' . "\r\n";
        $output .= 'CLASS:PUBLIC' . "\r\n";
        $output .= 'CREATED:' . date('Ymd\THis') . "\r\n";

        $description = strip_tags( htmlspecialchars_decode( $event->description ) );
        $description = str_replace(array( "\n", ';' ), array( '\n', '\;' ), $description);

        $output .= 'DESCRIPTION:' . $description . "\r\n";
        $output .= 'DTSTART:' . $dateEvent->format('Ymd\THis\Z') . "\r\n";
        $dateEvent->modify('+4 hour');
        $output .= 'DTEND:' . $dateEvent->format( 'Ymd\THis\Z' ) . "\r\n";
        $output .= 'DTSTAMP:' . $dateEvent->format('Ymd\THis\Z') . "\r\n";
        $output .= 'LAST-MODIFIED:' . date('Ymd\THis') . "\r\n";
        $output .= 'LOCATION:' . $event->location . "\r\n";
        $output .= 'UID:' . $dateEvent->format('Ymd\THis') . '-' . md5( $event->title ) . '@' . $_SERVER['SERVER_NAME'] . "\r\n";
        $output .= 'END:VEVENT' . "\r\n";
        $output .= 'END:VCALENDAR';

        header("Content-Type: text/Calendar;charset=utf-8");
        header('Content-Disposition: inline; filename="' . $filename . '.ics"');
/*      header('Content-Transfer-Encoding: binary');
        header('Expires: -1');
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header('Pragma: public');
        header('Content-Length: ' . mb_strlen( $output, '8bit' ) );
    */
        echo $output;
        exit;

这是输出

 BEGIN:VCALENDAR
 VERSION:2.0
 PRODID:-//My Company//SV-SE
 METHOD:PUBLISH
 BEGIN:VEVENT
 CLASS:PUBLIC
 CREATED:20130105T000547
 DESCRIPTION:This is my description.
 DTSTART:20130124T170000Z
 DTEND:20130124T210000Z
 DTSTAMP:20130124T210000Z
 LAST-MODIFIED:20130105T000547
 LOCATION:Top secret
 UID:20130124T210000-c88f2fb3f033284ec886aa15acb9eaee@example.com
 END:VEVENT
 END:VCALENDAR

我在Chrome和Firefox中获得的原因和错误消息的任何想法?

提前谢谢了

解决方法:

由于文件正在发送404标头状态,请尝试放置此标头:

header('HTTP/1.0 200 OK', true, 200);

标签:php,google-chrome,firefox,icalendar
来源: https://codeday.me/bug/20190629/1329639.html