By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,812 Members | 875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,812 IT Pros & Developers. It's quick & easy.

how can i make a download through php

P: n/a
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!!!
Apr 1 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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!!
Apr 1 '08 #2

P: n/a
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">click 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=\"".$name."\"");
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..espcially DWG/DXF!!
// Mind you we don't want to inmvoke plug-ins for these..
function get_mime($filename)
{
$default="application/force-download";
// first extract the extension
$array=explode(".",$filename); // split the name into the bits
separated by periods
$count=count($array);
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{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==$extension || $ext==$extension1 || $ext==$extension2 ||
$ext==$extension3 || $ext==$extension4 )
{
fclose ($fp);
return($mime_type);
}
}
fclose($fp);
return $default;
}
?>
=========

Hope this is of use.
Apr 1 '08 #3

P: n/a
On 1 Apr, 10:33, domnulnopcea <domnulnop...@gmail.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?
Apr 1 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.