0

I have a script which uploads files into an online directory and stores the file details in a database. The files when stored are renamed to the id of the entry in the database. Whenever a user requests a download, a simple SQL statement retrieves the file details from the database, the contents of the file are read from the database, and the file is prompted for download. The following is my code:

$one_file = $FILE_OBJECT->get($_GET['id']); // this is an object which just grabs the file details from the database


header("Content-type: ".$one_file['type']); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$one_file["filename"]."\""); // use 'attachment' to force a download
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$one_file["filename"]."\"");

readfile(_config('files_path').$_GET['id']);// reading the actual raw file stored in my online directory

Problem is that Im testing using a word document and its uploading perfectly - I've even checked the raw file being uploaded by manually changing its extension and it's uploading perfectly. The problem is that when it's downloaded using the code above, the Word file seems corrupted or something, because when I try to open it, it's all mumbled and jumbled. What's happening? I've used this snippet on a few other sites I've worked on, and they work perfectly fine... Help please!

cHao
  • 84,970
  • 20
  • 145
  • 172
Ali
  • 7,353
  • 20
  • 103
  • 161
  • Argh... **the**, the 'teh'. But as an question: (1) Can you confirm the storing in the database is done correctly, i.e: if you save that contents as a file on the filesystem, does it work? (2) What happens if I give `'../conf/yoursupersecretpasswordfile`' as `$_GET['id']`? – Wrikken Jul 23 '10 at 20:45
  • This post is hard to read when you use "teh" all the time. Correct spelling is not hard to come by especially with Firefox and Chrome having built-in spell checkers. If English is not your first language, well I would suggest a spell checker at any rate because it does deteriorate from your post and make it that much harder for someone to help / understand. (Not meaning to flame or anything, just providing some helpful critique for future posts) – Jim Jul 23 '10 at 20:45
  • 1
    Fixt. It bugged the hell out of me too. – cHao Jul 23 '10 at 20:48
  • sorry about that in a bad jam here :( – Ali Jul 23 '10 at 20:54

3 Answers3

2

By default PHP's header function will replace previous headers with the same name, so your first two headers are being overwritten by the second two. Delete the second two and see if that works.

Ian Wetherbee
  • 6,009
  • 1
  • 29
  • 30
  • Did that but didn't help at all.. the problem is somewhere within the above mentioned code snippet as the file is being stored fine on the online directory. – Ali Jul 23 '10 at 20:55
  • It might be an encoding issue with readfile, try adding `header("Content-Transfer-Encoding: binary");` Do other filetypes work? Try uploading a plain text file. – Ian Wetherbee Jul 23 '10 at 21:44
1

See if this helps:

Webkit and Excel file(PHPexcel)

Community
  • 1
  • 1
Nathan Loding
  • 3,185
  • 2
  • 37
  • 43
  • Thanjs man - I still don't know exactly why my code stopped working all of a suddent... – Ali Jul 25 '10 at 20:37
0

I was having the same problem: every time I downloaded a file, it was supposedly "corrupt". Turns out I had made a stupid directory path mistake, but the php error was being written into the downloaded file. Which, of course, made it "corrupt".

Actually I solved by reading Ian Wetherbee's comment about testing with a plain text file. Thanks Ian!

JakeParis
  • 11,056
  • 3
  • 42
  • 65