how can i make a download through php

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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!!!

Re: how can i make a download through php

domnulnopcea schreef:
Quoted text here. Click to load it

Show some code!!

Re: how can i make a download through php

domnulnopcea wrote:
Quoted text here. Click to load it
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">click to download</A>

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

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..
//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

//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=\"".$name."\"");
header("Content-Transfer-Encoding: binary");
print $content;
// be careful to leave no blank line after closing 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..espcially DWG/DXF!!
// Mind you we don't want to inmvoke plug-ins for these..
function get_mime($filename)
    // first extract the extension
    $array=explode(".",$filename); // split the name into the bits
separated by periods
    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($buffer) || $buffer=='#' || $buffer=='\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==$extension || $ext==$extension1 || $ext==$extension2 ||
$ext==$extension3 || $ext==$extension4 )
                    fclose ($fp);
    return $default;

Hope this is of use.

Re: how can i make a download through php

Quoted text here. Click to load it

What do you want the AJAX bit to do?

Site Timeline