files downloaded from DB are corrupt

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
NikBruce
Forum Newbie
Posts: 22
Joined: Wed Dec 23, 2009 5:16 am

files downloaded from DB are corrupt

Post by NikBruce »

Hi, when ever I download blobs from my DB I can't open them. I get I warning telling me that the files are either corrupt, damaged, truncated, or incomplete.

I can open txt and mp3 files.

I did open the damaged image files in photoshop, but I can only partially view them. So they are indeed damaged.

Is there a way to prevent the file corruption?

Thanks.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: files downloaded from DB are corrupt

Post by Darhazer »

You have error at line 117 :mrgreen:


Seriously, if you expect an answer, show your download code. And make sure that the blobs are inserted in the database correctly.
NikBruce
Forum Newbie
Posts: 22
Joined: Wed Dec 23, 2009 5:16 am

Re: files downloaded from DB are corrupt

Post by NikBruce »

hahahahaha my bad.

This is the upload code:

Code: Select all

$name = $_FILES['fileUpload']['name'];
$name = str_replace(" ","_", $name);
 
$mime = $_FILES['fileUpload']['type'];
 
$data = file_get_contents($_FILES  ['fileUpload']['tmp_name']);
 
$size = intval($_FILES['fileUpload']['size']);
 
$tmpName = $_FILES['fileUpload']['tmp_name'];
 
 
$ti = "INSERT INTO `$proName` (name, mime, size, data, createdDate, createdBy, discription)
VALUES ('$name', '$mime', '$size', $data, '$date', '$accName', '$discription')";
 
$result = mysql_query($ti) or die(mysql_error());
This is the download code:

Code: Select all

if(!is_numeric($id)) { die("Invalid id specified"); }
 
$sql = "SELECT `mime`, `data`, `size`, `name` FROM `$proName` WHERE `id` = '$id'";
 
$result = mysql_query($sql) or die(mysql_error());
 
$count = mysql_num_rows($result);
 
if($count == 1)
{
 
while($row = mysql_fetch_array($result))
{
$mime = $row["mime"];
$data = $row["data"];
$size = $row["size"];
$name = $row["name"];
 
header("Content-type: $mime");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");
header("Content-transfer-encoding: binary");
 
echo $data;
}//from the while loop
 
}//from if($count == 1)
 
else { echo "Record doesn't exist."; }
 
NikBruce
Forum Newbie
Posts: 22
Joined: Wed Dec 23, 2009 5:16 am

Re: files downloaded from DB are corrupt

Post by NikBruce »

solution:

the variables from the upload script were on a separate page to the rest of the upload script, and then passed along using a session. It will not work this way. they must be on the same page.
Post Reply