Re: how can i make a download through php
am 01.04.2008 12:40:33 von Erwin Moller
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!!
Re: how can i make a download through php
am 01.04.2008 13:28:56 von Courtney
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:
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..
==============
//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
============
// 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.
Re: how can i make a download through php
am 01.04.2008 13:42:22 von Captain Paralytic
On 1 Apr, 10:33, 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!!!
What do you want the AJAX bit to do?