Hi,
I got the following stand alone php script working for file downloads:
<?php
$file = "C:\\test\\asinglerose8ev.gif";
$basename = basename($file);
$file_extension = strtolower(substr(strrchr($basename,"."),1));
if (!file_exists($file))
{
print "ERROR: File not found. $basename";
exit;
}
else {
switch($file_extension)
{
case "PLS": $ctype="audio/x-scpls"; break;
case "pls": $ctype="audio/x-scpls"; break;
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint";
break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0,
pre-check=0");
header("Cache-Control: private",false); // required for certain
browsers
header("Content-Type: $ctype");
header("Content-Disposition: attachment;
filename=\"$basename\";" );
header("Content-Transfer-Encoding: binary");
readfile($file);
exit();
}
?>
I copied the code and created a new method in a cakephp controller:
function download2()
{
$file = "C:\\test\\asinglerose8ev.gif";
$basename = basename($file);
$file_extension = strtolower(substr(strrchr($basename,"."),1));
if (!file_exists($file))
{
print "ERROR: File not found. $basename";
exit;
}
else {
switch($file_extension)
{
case "PLS": $ctype="audio/x-scpls"; break;
case "pls": $ctype="audio/x-scpls"; break;
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint";
break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0,
pre-check=0");
header("Cache-Control: private",false); // required for certain
browsers
header("Content-Type: $ctype");
header("Content-Disposition: attachment;
filename=\"$basename\";" );
header("Content-Transfer-Encoding: binary");
readfile($file);
exit();
}
}
This cakephp method does not work. It is prefixing '\r\n' charecters
at the bigining of the downloaded file. So I cannot open image, pdf,
doc file.
I wasted a lot of time trying to figure out this problem.
Please suggest a work around. I really appreciate your help.
Thanks
Srini
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---