File Download NOT open

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jerryyang_la1@yahoo.com

    File Download NOT open

    I have a php page that reads files links from a Database.
    This works well, but when we click on the link the file starts to open.

    Is there anyway to force the file to SAVE not open ??

    thanks

  • Erwin Moller

    #2
    Re: File Download NOT open

    jerryyang_la1@y ahoo.com wrote:
    I have a php page that reads files links from a Database.
    This works well, but when we click on the link the file starts to open.
    >
    Is there anyway to force the file to SAVE not open ??
    >
    thanks
    Yes,

    You should send the right headers so the browser knows it should safe the
    file.
    Here is a piece of code that seems to work for me.

    ---------------------------
    $file_name = 'myexport.csv';

    // Create headers for save-as dialog
    header('Content-Type: text/comma-separated-values');
    //IE need specific header...
    if (strstr($_SERVE R['HTTP_USER_AGEN T'], 'MSIE'))
    {
    header('Content-Disposition: inline; filename="'.$fi le_name.'"');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    } else {
    header('Content-Disposition: attachment; filename="'.$fi le_name.'"');
    header('Pragma: no-cache');
    }


    ---------------------------

    That example was just for a csv file.
    You need another content-type for other kind of files.

    Maybe reading up here will help a bit:


    Good luck!

    Regards,
    Erwin Moller

    Comment

    • Jerry Stuckle

      #3
      Re: File Download NOT open

      jerryyang_la1@y ahoo.com wrote:
      I have a php page that reads files links from a Database.
      This works well, but when we click on the link the file starts to open.
      >
      Is there anyway to force the file to SAVE not open ??
      >
      thanks
      >
      Jerry,

      You can't force them to save it. That's up to the browser settings the
      user has entered, and you can't override those (which is a good thing).

      As Erwin indicated, if you are actually generating the files in PHP
      code, you need to set the headers appropriately. But if all you're
      doing is pointing to an existing file on the server, the server itself
      should set the appropriate header.

      They can always right click on the link and select "Save Link..." (exact
      wording depends on the browser). I'd recommend you add a note to the
      effect "To save the file, click your right mouse button on the link and
      select Save".

      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • Kim André Akerø

        #4
        Re: File Download NOT open

        Jerry Stuckle wrote:
        jerryyang_la1@y ahoo.com wrote:
        I have a php page that reads files links from a Database.
        This works well, but when we click on the link the file starts to
        open.

        Is there anyway to force the file to SAVE not open ??
        >
        [snip]
        >
        They can always right click on the link and select "Save Link..."
        (exact wording depends on the browser). I'd recommend you add a note
        to the effect "To save the file, click your right mouse button on the
        link and select Save".
        Something that's gonna be a slight problem for people using a Mac.

        --
        Kim André Akerø
        - kimandre@NOSPAM betadome.com
        (remove NOSPAM to contact me directly)

        Comment

        • Jerry Stuckle

          #5
          Re: File Download NOT open

          Kim André Akerø wrote:
          Jerry Stuckle wrote:
          >
          >
          >>jerryyang_la1 @yahoo.com wrote:
          >>
          >>>I have a php page that reads files links from a Database.
          >>>This works well, but when we click on the link the file starts to
          >>>open.
          >>>
          >>>Is there anyway to force the file to SAVE not open ??
          >>
          >>[snip]
          >>
          >>They can always right click on the link and select "Save Link..."
          >>(exact wording depends on the browser). I'd recommend you add a note
          >>to the effect "To save the file, click your right mouse button on the
          >>link and select Save".
          >
          >
          Something that's gonna be a slight problem for people using a Mac.
          >
          Isn't there an equivalent operation for Macs?

          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          • Chuck Anderson

            #6
            Re: File Download NOT open

            jerryyang_la1@y ahoo.com wrote:
            I have a php page that reads files links from a Database.
            This works well, but when we click on the link the file starts to open.
            >
            Is there anyway to force the file to SAVE not open ??
            >
            thanks
            >
            >
            Here's what I use for a PDF file. Create a script and "present" the file
            through it.

            <?php

            header('Content-Description: File Transfer');
            header('Content-Type: application/pdf'); // change this to your mime type
            header('Content-Length: ' . filesize($file) );

            // to download
            header('Content-Disposition: attachment; filename=' . basename($file) );

            // to open in browser
            // header('Content-Disposition: inline; filename=' . basename($file) );

            readfile($file) ;
            ?>

            --
            *************** **************
            Chuck Anderson • Boulder, CO

            *************** **************

            Comment

            • Kim André Akerø

              #7
              Re: File Download NOT open

              Jerry Stuckle wrote:
              Kim André Akerø wrote:
              Jerry Stuckle wrote:

              jerryyang_la1@y ahoo.com wrote:
              >
              I have a php page that reads files links from a Database.
              This works well, but when we click on the link the file starts
              to open.

              Is there anyway to force the file to SAVE not open ??
              >
              [snip]
              >
              They can always right click on the link and select "Save Link..."
              (exact wording depends on the browser). I'd recommend you add a
              note to the effect "To save the file, click your right mouse
              button on the link and select Save".
              Something that's gonna be a slight problem for people using a Mac.
              >
              Isn't there an equivalent operation for Macs?
              Ctrl+click, I think. (Hold the CTRL key and click)

              --
              Kim André Akerø
              - kimandre@NOSPAM betadome.com
              (remove NOSPAM to contact me directly)

              Comment

              Working...