View Full Version : Problem pulling file from mySQL field

04-14-2004, 04:11 AM
When I download a file using this script, it always ends up the right size, but somehow corrupted, however, the commented out code below it works and saves a file from the SQL field to a file local to the server, and thus proves that the file stored in the mySQL db is fine. I'm not sure what is happening here....any help would be much appreciated.

PS - I addslashes when I store the file in the bin_data field, but if I stripslashes when I pull it out, neither code sample works.

$LoanID = 'Black01';
if ($LoanID) {
include "Connect.php";

$sql = "SELECT bin_data, filetype, filename, filesize FROM attachments WHERE LoanID='$LoanID'";

$result = @mysql_query($sql);
$data = @mysql_result($result, 0, "bin_data");
$name = @mysql_result($result, 0, "filename");
$size = @mysql_result($result, 0, "filesize");
$type = @mysql_result($result, 0, "filetype");

header("Content-type: $type");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");
header("Content-Description: PHP Generated Data");
echo $data;

$thefile = "testDL.zip";
$fp = fopen($thefile,"w");

04-14-2004, 06:56 AM
Well, the DB code has nothing to do with this,

as replacing

$data = @mysql_result($result, 0, "bin_data");


$data = fread(fopen("testDL.zip", "rb"), filesize("testDL.zip"));

yields the same result, a corrupted file on download....I don't get it, I must be screwing something up.....when I send the data, because the HTML headers work, I don't see how I can mess up echo $data though...

04-14-2004, 09:07 PM
Why is the file stored inside the db? Can't you just store it in your filesystem and onlys store the fileadress in the db ?

04-14-2004, 09:14 PM
I'm not sure what the problem is exactly, but I do have a cool script for uploading files, listing the files in the db, and allowing for download.

This may not be of much use to you depending on how your stuff is setup, because it may in fact require you to redo everything.. but that might be okay too?

I can't remember where i got it from, but i've attached the files nonetheless. I've also included the database schema required. Feel free to play with it as you like.

Hope that helps,

04-14-2004, 09:21 PM
raf et al,

Just wondering the reasons for and against storing files in the db and on the file system.

I suppose it depends on the application too. I've only had one experience, where I've got an information system in which there are many client records. Each of these records will have files associated with them (in some cases 20+ files) all with different properties and meanings. In this sort of scenario I prefer to store the files in the database as the only time you would view this file is through the client record while in the system.

Also, naming the files is something my clients do not want to deal with. I guess it's easy enough to give a unique name to these files...

Anyway, just wondering..


04-15-2004, 12:19 AM
I'm storing the files in a database becuase they are small <300K each, and there will never be more than 20 or so stored at any one time. Also, so I can secure them a little better without having to have the admin screw with .htaccess. The auth system I wrote only works on php files, so I don't really want to have any other(customer) files local.

I've debugged to the point that I've determined the database storage/retrieval code is fine, but there is a bug in the way I am trying to download the data with Content-Disposition header I think.....according to php manual there is an IE bug that sometimes causes problem with this.

Anyways, sad69, I appreciate the thought, but the download.php script in your example is missing from the zipfile. the listing.php shows links to download.php I would appreciate if you could send me a copy of that download.php so I can compare it to my own.....


04-15-2004, 12:32 AM
Oh man, I'm sorry about that.. it should be part of the new attachment.


04-15-2004, 09:55 AM
Well, I'm officially at a loss, my code basically does the same thing as the example code posted. It's not a problem with the Content-Disposition header, or I simply wouldn't get prompted to download the file.....

could VERY possibly be a problem in my code somewhere, but I can't seem to see one, after MUCH debugging, hoping maybe this is an apache win32 prob, but if so, someone else would've probably already known about it.

After I set the headers, I've tried echo $, I've tried, readfile("testDL.zip") , both yield the same result, a correctly sized, but corrupt downloaded file.

I've tried reading source file from DB, I've tried reading directly for source file on server - same results.

WINDIFF shows that it apparently is adding a blank line at the beginning of every file that gets downloaded with this script....grrrhh.

If anyone is bored, Here is the code, pretty lean and pruned, just download.php, upload.php, and listing.php. Also included file to create table with...

Thanks again guys....

04-16-2004, 12:03 AM
turns out I had one space after the closing ?> tag on my download page....removed space and all is well....

thanks for the help guys...