using header functions in php

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

    using header functions in php

    hi,

    i am trying to create code that will prompt the user to download.
    below is the code i am using:


    ..
    ..
    ..

    $content = datadump ("clients");

    $file_name = "MySQL_Database _Backup.sql";
    Header("Content-type: application/octet-stream");
    Header("Content-Disposition: attachment; filename=$file_ name");


    'datadunp' is simply a function that i have created to generate a sql
    string from my databases and return the value into $content.

    unfortunately, when i run this code from the server, it doesnt do
    anything. i have noticed that i am gettin errors when i place the code
    with the header function anywhere but the top of the document but even
    without error, it isnt doin anything.

    btw, i got this code from a web page
    (http://www.devpapers.com/article/55) and modified it as i dont really
    have a great knowledge of header functions in php. any help would be
    great. cheers

    burnsy
  • Brian

    #2
    Re: using header functions in php

    mr_burns wrote:
    [color=blue]
    > i am trying to create code that will prompt the user to download.[/color]

    [It looks like your [shift] key is broken. You should have that fixed.]

    There's no way to force a download (or much of anything on the www).
    Your best bet is to honestly declare the MIME type and let the browser
    handle it from there.
    [color=blue]
    > Header("Content-type: application/octet-stream");
    > Header("Content-Disposition: attachment; filename=$file_ name");[/color]
    [color=blue]
    > i have noticed that i am gettin errors when i place the code with the
    > header function anywhere but the top of the document[/color]

    You must send headers before any character data -- including whitespace
    -- is sent.
    [color=blue]
    > but even without error, it isnt doin anything.[/color]

    You're sending data after the headers, right? So this data is sent by
    the server, but the browser does nothing with it?

    --
    Brian (remove ".invalid" to email me)

    Comment

    • chotiwallah

      #3
      Re: using header functions in php

      bissatch@yahoo. co.uk (mr_burns) wrote in message news:<651c6ea9. 0408121129.6f53 bdc1@posting.go ogle.com>...[color=blue]
      > hi,
      >
      > i am trying to create code that will prompt the user to download.
      > below is the code i am using:
      >
      >
      > .
      > .
      > .
      >
      > $content = datadump ("clients");
      >
      > $file_name = "MySQL_Database _Backup.sql";
      > Header("Content-type: application/octet-stream");
      > Header("Content-Disposition: attachment; filename=$file_ name");
      >
      >
      > 'datadunp' is simply a function that i have created to generate a sql
      > string from my databases and return the value into $content.
      >
      > unfortunately, when i run this code from the server, it doesnt do
      > anything. i have noticed that i am gettin errors when i place the code
      > with the header function anywhere but the top of the document but even
      > without error, it isnt doin anything.
      >
      > btw, i got this code from a web page
      > (http://www.devpapers.com/article/55) and modified it as i dont really
      > have a great knowledge of header functions in php. any help would be
      > great. cheers
      >
      > burnsy[/color]

      header("Content-type: application/text");

      header("Content-Disposition: attachment; filename=".$fil e_name);

      print $all_data;

      important: you have to send the download header before anything else
      to the browser. so if your datadump creates any output the download
      header becomes invalid. if datadump shall create output before the
      header is sent, use ob_start() to buffer and ob_flush() to release the
      output after the header is sent.

      micha

      Comment

      • John Dunlop

        #4
        Re: using header functions in php

        chotiwallah wrote:
        [color=blue]
        > bissatch@yahoo. co.uk (mr_burns) wrote
        >[color=green]
        > > i am trying to create code that will prompt the user to download.[/color][/color]

        [ ... ]
        [color=blue]
        > header("Content-type: application/text");[/color]

        RFC2616 discourages the use of non-registered media types.

        Below is an excerpt from Brian's article, in case it didn't
        reach you.

        | There's no way to force a download (or much of anything on
        | the www). Your best bet is to honestly declare the MIME
        | type and let the browser handle it from there.

        news:10hod3rkde 4913@corp.super news.com

        [ ... ]

        --
        Jock

        Comment

        • Brian

          #5
          Re: using header functions in php

          John Dunlop wrote:
          [color=blue]
          > chotiwallah wrote:
          >[color=green]
          >> header("Content-type: application/text");[/color]
          >
          >
          > RFC2616 discourages the use of non-registered media types.[/color]

          And if you are going to do something non-standard, at least use an "x-"
          prefix.

          --
          Brian (remove ".invalid" to email me)

          Comment

          • R. Rajesh Jeba Anbiah

            #6
            Re: using header functions in php

            bissatch@yahoo. co.uk (mr_burns) wrote in message news:<651c6ea9. 0408121129.6f53 bdc1@posting.go ogle.com>...
            <snip>[color=blue]
            > unfortunately, when i run this code from the server, it doesnt do
            > anything. i have noticed that i am gettin errors when i place the code
            > with the header function anywhere but the top of the document but even
            > without error, it isnt doin anything.[/color]

            0. Define exactly _what_ did work and what didn't work?
            1. Manual and usernotes are your friend: www.php.net/<keyword>
            2. http://in2.php.net/header & http://in2.php.net/outcontrol
            3. MSIE needs different headers to be sent to trigger download. Refer
            usernotes at http://in2.php.net/header
            4. If you call download url in single browser window, it won't work.
            You have to enter the url from some other anchor tags by clicking.

            --
            | Just another PHP saint |
            Email: rrjanbiah-at-Y!com

            Comment

            Working...