how can i make a download through php

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

    how can i make a download through php

    i have a dinamically generated file! and i have a button on my page...
    i want to download the file by ajax when i click the button! i tried
    to send the headers in the xhtmlrequest but my browser does not pop up
    that dialog box to download the file!!

    thank you!!!
  • Erwin Moller

    #2
    Re: how can i make a download through php

    domnulnopcea schreef:
    i have a dinamically generated file! and i have a button on my page...
    i want to download the file by ajax when i click the button! i tried
    to send the headers in the xhtmlrequest but my browser does not pop up
    that dialog box to download the file!!
    >
    thank you!!!
    Show some code!!

    Comment

    • The Natural Philosopher

      #3
      Re: how can i make a download through php

      domnulnopcea wrote:
      i have a dinamically generated file! and i have a button on my page...
      i want to download the file by ajax when i click the button! i tried
      to send the headers in the xhtmlrequest but my browser does not pop up
      that dialog box to download the file!!
      >
      thank you!!!
      Include in your page a URL that points to a php program with a parameter
      to identify the file you want to generate, and make a php programm to
      send it.



      In my code I have this:

      <A HREF="filesend. php?id=437">cli ck to download</A>

      That invokes the filesned.php program and requests file id 437 in this
      instance.

      The guts of filesend.php are these.

      $name, $content are the 'name' and the actual content of a file.
      get_mime() is a function that uses my linux list of extenstion->mime
      type mappings to set up a mime type.


      Filesend.php the main features..
      ==============
      <?php
      //omitted from here is a load of SQL preamble that
      //gets the file name and content from an SQL database
      //according to the GET id=437 sent to this program


      $mtype=get_mime ($name);
      //spit out standard header stuff
      header("Pragma: public");
      header("Expires : 0");
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
      header("Cache-Control: public");
      header("Content-Description: File Transfer");
      header("Content-Type: ".$mtype);
      header("Content-Disposition: attachment; filename=\"".$n ame."\"");
      header("Content-Transfer-Encoding: binary");
      print $content;
      ?>
      // be careful to leave no blank line after closing PHP


      MIME_LIB.php
      ============
      <?php
      // looks up mime type in /etc/mime.types and returns the type, or a
      default if unmatched
      // makes no attempt to interrogate the file content as such.
      // THIS NEEDS MORE WORK!!! it doesn't get all types..espciall y DWG/DXF!!
      // Mind you we don't want to inmvoke plug-ins for these..
      function get_mime($filen ame)
      {
      $default="appli cation/force-download";
      // first extract the extension
      $array=explode( ".",$filena me); // split the name into the bits
      separated by periods
      $count=count($a rray);
      if ($count<2) // if there IS NO extension..
      return $default; // and let the user sort it out.
      $ext=$array[$count-1]; // it will be the last element in the array..
      $fp=fopen("/etc/mime.types", "r");
      if(!$fp) return ($default); // no /etc/mime.types file
      while (!feof($fp))
      {
      $buffer = fgets($fp, 128);
      if (ctype_space($b uffer{0}) || $buffer{0}=='#' || $buffer{0}=='\n ')
      continue; // skip empty lines. or lines starting with spaces
      or hashes
      sscanf($buffer, "%s %s %s %s %s %s \n",$mime_type, $extension,
      $extension1, $extension2, $extension3, $extension4);
      if ($ext==$extensi on || $ext==$extensio n1 || $ext==$extensio n2 ||
      $ext==$extensio n3 || $ext==$extensio n4 )
      {
      fclose ($fp);
      return($mime_ty pe);
      }
      }
      fclose($fp);
      return $default;
      }
      ?>
      =========

      Hope this is of use.

      Comment

      • Captain Paralytic

        #4
        Re: how can i make a download through php

        On 1 Apr, 10:33, domnulnopcea <domnulnop...@g mail.comwrote:
        i have a dinamically generated file! and i have a button on my page...
        i want to download the file by ajax when i click the button! i tried
        to send the headers in the xhtmlrequest but my browser does not pop up
        that dialog box to download the file!!
        >
        thank you!!!
        What do you want the AJAX bit to do?

        Comment

        Working...