PHP Download Links Corrupting.. Please help..
Posted: Sun Sep 19, 2010 2:36 pm
Hello friends,
I've been setting up an online store using FoxyCart, and after a long weekend of trying to get the "Host your own downloadable products" script to work, I finally have everything working fine.
However, there's one problem...
When a link is generated, the file downloads okay but is corrupted/invalid. I need to use Zip files as there will be several files within one folder per product. I've checked the zip files directly from the server and they unzip just fine, but whenever they are accessed via the php script, they won't open.
Below is part of the code. I have added the "zip" section myself, as the browser wouldn't acknowledge the file as a zip beforehand (it thought every file was a jpg). I'm a PHP newbie, and so far clueless as to what's going wrong.
I hope someone can help me
Thanks, Bee x
//Download from filesystem - recommended
if ($fd = fopen ($location, "r"))
{
$fsize = filesize($location);
$path_parts = pathinfo($location);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf"); // add here more headers for diff. extensions
case "zip": header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;
default;
header('Content-Type: application/x-download');
header("Content-Disposition: attachment; filename=\"$file_name\"");
}
header("Pragma: cache");
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
print $buffer;
}
}
fclose ($fd);
}
exit;
?>
I've been setting up an online store using FoxyCart, and after a long weekend of trying to get the "Host your own downloadable products" script to work, I finally have everything working fine.
However, there's one problem...
When a link is generated, the file downloads okay but is corrupted/invalid. I need to use Zip files as there will be several files within one folder per product. I've checked the zip files directly from the server and they unzip just fine, but whenever they are accessed via the php script, they won't open.
Below is part of the code. I have added the "zip" section myself, as the browser wouldn't acknowledge the file as a zip beforehand (it thought every file was a jpg). I'm a PHP newbie, and so far clueless as to what's going wrong.
I hope someone can help me
Thanks, Bee x
//Download from filesystem - recommended
if ($fd = fopen ($location, "r"))
{
$fsize = filesize($location);
$path_parts = pathinfo($location);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf"); // add here more headers for diff. extensions
case "zip": header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;
default;
header('Content-Type: application/x-download');
header("Content-Disposition: attachment; filename=\"$file_name\"");
}
header("Pragma: cache");
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
print $buffer;
}
}
fclose ($fd);
}
exit;
?>