...

View Full Version : Problem pulling file from mySQL field



black3842
04-14-2004, 03: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.

<?php
$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");
fwrite($fp,$data);
fclose($fp);
*/
}
?>

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

as replacing

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

with

$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...

raf
04-14-2004, 08: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 ?

sad69
04-14-2004, 08: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,
Sadiq.

sad69
04-14-2004, 08: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..

Sadiq.

black3842
04-14-2004, 11:19 PM
raf
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.....

Thanks,

sad69
04-14-2004, 11:32 PM
Oh man, I'm sorry about that.. it should be part of the new attachment.

Sadiq.

black3842
04-15-2004, 08: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....

black3842
04-15-2004, 11:03 PM
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...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum