Headers for streaming files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Markus Ernst

    Headers for streaming files

    Hi

    I know this question is rather HTTP related than PHP, but I did not find an
    HTTP group on my news server.

    I deliver some files with the following PHP syntax:

    header('Content-Description: File Transfer');
    header('Content-Type: '.$mimetype);
    header('Content-Length: '.filesize($fil e));
    header('Content-Disposition: attachment; filename='.$fil ename);
    readfile($file) ;

    That works well with file types that need to be downloaded to disk. Anyway
    if the file is a MP3 file, both IE and Mozilla do the same, they prompt me
    to decide if I want to save it to disk, and then download the file entirely
    before opening a player.

    I sniffed for the differences in headers when a normal link to an mp3 file
    is clicked and both tested UAs start streaming and playing immediately:
    There is no Content-Disposition header. Removing this header, or just the
    "attachment " part of it, results in the expected streaming behaviour, but
    the filename is lost, and the script's filename is used instead.

    Is it possible to serve the filename without a Content-Disposition header?
    Or is there an alternative to the value "attachment ", which still allows me
    to deliver a filename?

    Thanks for a hint
    Markus


  • Daniel Tryba

    #2
    Re: Headers for streaming files

    Markus Ernst <derernst@no#sp #amgmx.ch> wrote:[color=blue]
    > There is no Content-Disposition header. Removing this header, or just the
    > "attachment " part of it, results in the expected streaming behaviour, but
    > the filename is lost, and the script's filename is used instead.
    >
    > Is it possible to serve the filename without a Content-Disposition header?
    > Or is there an alternative to the value "attachment ", which still allows me
    > to deliver a filename?[/color]

    What about generating URL with the desired filename? There are a couple
    of ways to do this (depending on webserver) but easiest is propably with
    (apache) MultiViews:
    /foo/stream/filename
    where stream.php is in the dir foo. The script can retrieve filename
    from $_SERVER['PATHINFO'] (IIRC).

    The rewrite engine is another easy solution.

    --

    Daniel Tryba

    Comment

    • Chung Leong

      #3
      Re: Headers for streaming files

      "Markus Ernst" <derernst@NO#SP #AMgmx.ch> wrote in message news:<41826d37$ 0$29213$afc38c8 7@news.easynet. ch>...[color=blue]
      > Hi
      >
      > I know this question is rather HTTP related than PHP, but I did not find an
      > HTTP group on my news server.
      >
      > I deliver some files with the following PHP syntax:
      >
      > header('Content-Description: File Transfer');
      > header('Content-Type: '.$mimetype);
      > header('Content-Length: '.filesize($fil e));
      > header('Content-Disposition: attachment; filename='.$fil ename);
      > readfile($file) ;
      >
      > That works well with file types that need to be downloaded to disk. Anyway
      > if the file is a MP3 file, both IE and Mozilla do the same, they prompt me
      > to decide if I want to save it to disk, and then download the file entirely
      > before opening a player.
      >
      > I sniffed for the differences in headers when a normal link to an mp3 file
      > is clicked and both tested UAs start streaming and playing immediately:
      > There is no Content-Disposition header. Removing this header, or just the
      > "attachment " part of it, results in the expected streaming behaviour, but
      > the filename is lost, and the script's filename is used instead.[/color]

      Try "Content-Disposition: inline; filename=$filen ame".

      Comment

      • Markus Ernst

        #4
        Re: Headers for streaming files

        Chung Leong wrote:[color=blue]
        > "Markus Ernst" <derernst@NO#SP #AMgmx.ch> wrote in message
        > news:<41826d37$ 0$29213$afc38c8 7@news.easynet. ch>...[color=green]
        >> Hi
        >>
        >> I know this question is rather HTTP related than PHP, but I did not
        >> find an HTTP group on my news server.
        >>
        >> I deliver some files with the following PHP syntax:
        >>
        >> header('Content-Description: File Transfer');
        >> header('Content-Type: '.$mimetype);
        >> header('Content-Length: '.filesize($fil e));
        >> header('Content-Disposition: attachment; filename='.$fil ename);
        >> readfile($file) ;
        >>
        >> That works well with file types that need to be downloaded to disk.
        >> Anyway if the file is a MP3 file, both IE and Mozilla do the same,
        >> they prompt me to decide if I want to save it to disk, and then
        >> download the file entirely before opening a player.
        >>
        >> I sniffed for the differences in headers when a normal link to an
        >> mp3 file is clicked and both tested UAs start streaming and playing
        >> immediately: There is no Content-Disposition header. Removing this
        >> header, or just the "attachment " part of it, results in the expected
        >> streaming behaviour, but the filename is lost, and the script's
        >> filename is used instead.[/color]
        >
        > Try "Content-Disposition: inline; filename=$filen ame".[/color]

        Thank you Chung. I tried this, but it did not help as the filename only
        seems to have an effect with the attachment setting.

        --
        Markus


        Comment

        • Markus Ernst

          #5
          Re: Headers for streaming files

          Daniel Tryba wrote:[color=blue]
          > Markus Ernst <derernst@no#sp #amgmx.ch> wrote:[color=green]
          >> There is no Content-Disposition header. Removing this header, or
          >> just the "attachment " part of it, results in the expected streaming
          >> behaviour, but the filename is lost, and the script's filename is
          >> used instead.
          >>
          >> Is it possible to serve the filename without a Content-Disposition
          >> header? Or is there an alternative to the value "attachment ", which
          >> still allows me to deliver a filename?[/color]
          >
          > What about generating URL with the desired filename? There are a
          > couple of ways to do this (depending on webserver) but easiest is
          > propably with (apache) MultiViews:
          > /foo/stream/filename
          > where stream.php is in the dir foo. The script can retrieve filename
          > from $_SERVER['PATHINFO'] (IIRC).
          >
          > The rewrite engine is another easy solution.[/color]

          Thank you Daniel. I was not aware of MultiViews and will read more on this
          interesting possibility. I thought about using the rewrite engine before; I
          use it for creating search-engine friendly urls already. Anyway I am not too
          familiar with using .htaccess and also don't want to rely on it too much for
          non-optional stuff, as the application must be installable on servers with
          various configurations.

          I think what I will do is actually re-write the application and store the
          files with their actual filenames (and validate these on upload), instead of
          storing them with custom names and giving the filename back on download, as
          I tried. So I won't have to handle the headers at all, as I can simply link
          the files.

          --
          Markus


          Comment

          • Michael Fesser

            #6
            Re: Headers for streaming files

            .oO(Markus Ernst)
            [color=blue]
            >I sniffed for the differences in headers when a normal link to an mp3 file
            >is clicked and both tested UAs start streaming and playing immediately:
            >There is no Content-Disposition header. Removing this header, or just the
            >"attachment " part of it, results in the expected streaming behaviour, but
            >the filename is lost, and the script's filename is used instead.[/color]

            For streaming MP3s there's another way:



            Micha

            Comment

            • Markus Ernst

              #7
              Re: Headers for streaming files

              Michael Fesser wrote:[color=blue]
              >
              > For streaming MP3s there's another way:
              >
              > http://www.developingwebs.net/html/mp3.php
              >[/color]

              Thank you for pointing me to this interesting tutorial. This method looks
              similar to what you have to do with RealAudio files. Do you know what is the
              advantage of using an intermediate text file which points to the audio
              source?

              --
              Markus


              Comment

              • Michael Fesser

                #8
                Re: Headers for streaming files

                .oO(Markus Ernst)
                [color=blue]
                >Michael Fesser wrote:[color=green]
                >>
                >> For streaming MP3s there's another way:
                >>
                >> http://www.developingwebs.net/html/mp3.php
                >>[/color]
                >Thank you for pointing me to this interesting tutorial. This method looks
                >similar to what you have to do with RealAudio files. Do you know what is the
                >advantage of using an intermediate text file which points to the audio
                >source?[/color]

                The browser only downloads the little text file with the resource
                pointer, the download of the media file is handled directly by the
                associated application, which will take care of the streaming and maybe
                other things (buffering for example).

                If you link directly to the media file instead the browser will probably
                download it first, before handing over to the player.

                Micha

                Comment

                Working...