PHP Download Links Corrupting.. Please help..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bee
Forum Newbie
Posts: 5
Joined: Sun Sep 19, 2010 2:25 pm

PHP Download Links Corrupting.. Please help..

Post by bee »

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;
?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP Download Links Corrupting.. Please help..

Post by Jonah Bron »

Try taking just the header functions and other bare necessities and putting them into a seperate file to see if you can get the basics working. Post the code of what you come up with (please use [syntax=php][/syntax] tags).
bee
Forum Newbie
Posts: 5
Joined: Sun Sep 19, 2010 2:25 pm

Re: PHP Download Links Corrupting.. Please help..

Post by bee »

I'm certainly not knowledgeable enough about PHP to even attempt it unfortunately :(

This script is supposed to work "as is" - I'm very confused by it all... I was hoping there would just be a line or two to add which would allow the zip files to download and unpack successfully, but maybe there's more to it than that?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP Download Links Corrupting.. Please help..

Post by Jonah Bron »

Okay, so you didn't write it? Did you make any changes at all to it?
bee
Forum Newbie
Posts: 5
Joined: Sun Sep 19, 2010 2:25 pm

Re: PHP Download Links Corrupting.. Please help..

Post by bee »

only the line in bold:
case "zip": header("Content-type: application/zip");
which seemed to have stopped my browser thinking that the file was a jpg...
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP Download Links Corrupting.. Please help..

Post by Jonah Bron »

You might read this, it looks like somebody else had a problem with this.

http://www.codingforums.com/archive/ind ... 57392.html
bee
Forum Newbie
Posts: 5
Joined: Sun Sep 19, 2010 2:25 pm

Re: PHP Download Links Corrupting.. Please help..

Post by bee »

Thanks Jonah, I appreciate you finding me that :)

Perhaps the conclusion then is that there is no code to change?

Encode in UTF-8 without BOM though? Not sure how to do that on Mac OSX, and I have audio files in the zip too.. Any ideas there?

Thanks again.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP Download Links Corrupting.. Please help..

Post by Jonah Bron »

Try this code instead.

Code: Select all

//Download from filesystem - recommended
if (!file_exists($location)) {
    die('File does not exist');
}

$fsize = filesize($location);
$path_parts = pathinfo($location);
$ext = strtolower($path_parts["extension"]);

if ($ext != 'zip') {
    die('Wrong file type');
}

header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
header("Pragma: cache");
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly

readfile($location);
exit;
?>
bee
Forum Newbie
Posts: 5
Joined: Sun Sep 19, 2010 2:25 pm

Re: PHP Download Links Corrupting.. Please help..

Post by bee »

Many thanks for the code.. I tried it, but the screen just went blank (instead of starting the download).. GOOD NEWS THOUGH; I think I might have made a break through!

I don't think the script is at fault after all (my hosts told me it was the script). I think it is a PHPmysql database error as I've just re-uploaded and reconfigured all the files on a test server and the zip has just downloaded fine.

Thank you so much for your efforts! Bee x :D
Post Reply