storing files in mysql longblog



Hi, I've set up a site where I can upload a file into a mysql db. The file seems to upload ok, but when I go to download it, it is corrupted (txt files are ok, but images, word and excel files aren't). If I look at the blob in mysqlfront, the images appear ok, so it seems that they upload ok, but sounds like there's a problem with my download script.

I've tried as many options as I can think of - like trying fopen with\without the binary option, addslashes, base64_encode, using\not using stripslashes, using diff header options...

Is there anything you can suggest? Am I doing something wrong somewhere?

Hope ya can help! I've spent ages trying to get this working!





if (isset($object_id)) {

include "conn.php";
$sql = "SELECT file_data, file_type, file_name, file_size FROM docs WHERE object_id=$object_id";
$result = @mysql_query($sql, $db);
$row = mysql_fetch_array($result);
$data = $row["file_data"];
$name = $row["file_name"];
$size = $row["file_size"];
$type = $row["file_type"];

header("Content-type: $type");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");
header("Content-Description: PHP Generated Data");
# header("Content-transfer-encoding: binary");

echo stripslashes($data);

#echo $data;





if ($action == "upload") {
// ok, let's get the uploaded data and insert it into the db now
include "detail.php";

if (isset($upload_file) && $upload_file != "none") {
$data = addslashes(fread(fopen($upload_file, "rb"), filesize($upload_file)));
$strDescription = addslashes(nl2br($file_description));
$sql = "INSERT INTO docs ";
$sql .= "(description, file_data, file_name, file_size, file_type, date_uploaded) ";
$sql .= "VALUES ('$strDescription', '$data', ";
$sql .= "'$upload_file_name', '$upload_file_size', '$upload_file_type', sysdate())";
$result = mysql_query($sql, $db);

# echo $sql;
mysql_free_result($result); // it's always nice to clean up!

echo "File has been uploaded successfully.<br><br>";

echo "<a href='files.php'>View Files</a>";

} else {


<FORM METHOD="post" ACTION="upload.php" ENCTYPE="multipart/form-data">
<INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="1000000">
<INPUT TYPE="hidden" NAME="action" VALUE="upload">
<TD>Description: </TD>
<TD><TEXTAREA NAME="file_description" ROWS="10" COLS="50"></TEXTAREA></TD>
<TD>File: </TD>
<TD><INPUT TYPE="file" NAME="upload_file"></TD>
<TD COLSPAN="2"><INPUT TYPE="submit" VALUE="Upload"></TD>
What do you mean by "corrupted"? Are they zero-length? If you open them in a text editor is there any text at the top that shouldn't be there?

I'm suspicious of your use of stripslashes($data). There should be no reason whatsoever to use stripslashes(), and I can easily imagine that mangling the data.
Say if I have a Word file, once it's downloaded, Word doesn't recognise it as a Word file and tries to run the text converter. The length is correct - but something in the file has changed to prevent Word from recognising it. Same with pictures (but they just say the image is corrupted).

I've tried to do it with and without stripslashes, but the same thing happens. Is it something to do with there being a difference between binary and text files? (because .txt files work ok). Like I say, the image preview in mySqlFront can display an uploaded image ok, which suggests the problem is with the downloading. Am I missing any code in the download function?

Thanks for your help.
Did you do what I suggested and open the downloaded file in a text editor and see if there's any text at the top that shouldn't be there?
what are u doing with fopen in the upload.php file u should be rather using $_FILES array
Both files appear to be exactly the same, except the downloaded file has three carriage return symbols (the rect. boxes) at the start.

Could that be what's causing the problem? I can't see anything in the code that would be doing it.
ruturajv, i think depending on the way php is set up, $upload_file can be used instead of $_FILES[]...i've tried both in this case and neither work.
Yes, the carriage returns could very likely be the problem. Make sure that PHP is not outputting anything before $data. Make sure there isn't any whitespace before the opening <?php tag.
problem solved! i discovered the included file detail.php had a few blank lines after the ?> that was being added on to the file...I'm kicking myself now for not noticing quicker. many thanks for the help.