0

I would like to generate an "on-the-fly-generated" vtt file with PHP.

My code is as follows:

<?php
header('Content-Type: text/vtt; charset=utf-8');
header('Content-Disposition: inline; filename=data.vtt');
include 'connect.php';
if (isset($_GET["dbname"]))
{
$dbname = $_GET["dbname"];
}
if (isset($_GET["topnr"]))
{
$topnr = $_GET["topnr"];
}
$output = fopen('php://output', 'w');
$query = "SELECT * FROM `$dbname` WHERE `TOPNrReal` = $topnr LIMIT 0 , 1" ;
            $queryOut = mysqli_query($con, $query);
            while($row = mysqli_fetch_object($queryOut))
   {
    $vtt = $row->vtt;
   }

fwrite($output, ltrim(utf8_encode($vtt)));
?>

This generates a dynamical vtt file.

The problem is now, that there are two leading whitespaces at the beginning of this file:

  WEBVTT

00:00:00.000 --> 00:00:01.509
<i>Topic
16: First Topic</i>

I figured out, that tis issue occurs if the first header tag is set. But without this tag, there are other problems.

Is there any solution to eliminate this first two whitespaces?

Thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MZL
  • 111
  • 1
  • 8
  • use ```echo``` instead of writing in ```php://output``` .... ie: ```...{$vtt = $row->vtt;} echo $vtt; exit();``` – PersianMan Oct 17 '18 at 11:40
  • 2- There is nothing before the ``` – PersianMan Oct 17 '18 at 11:44
  • I got the solution. I had two whitespaces after the ?> closing tag in the include file. – MZL Oct 17 '18 at 11:44
  • Possible duplicate of [Why would one omit the close tag?](https://stackoverflow.com/questions/4410704/why-would-one-omit-the-close-tag) – Nigel Ren Oct 17 '18 at 11:52
  • It is good practice to skip closing php tag `?>` because it can create problems, and as php framework also skips closing tag like laravel, zend, codeigniter etc. – Haritsinh Gohil Oct 17 '18 at 12:08

0 Answers0