If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > storing files in mysql longblob

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-17-04, 09:46
scottst scottst is offline
Registered User
 
Join Date: Nov 2004
Posts: 10
storing files in mysql longblob

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!

Thanks

-----------------------------------------------------------------------------------

Download.php

<?php

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;

}

?>

Upload.php

<?php

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>";

}
mysql_close();
} else {

?>

<HTML>
<BODY>
<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">
<TABLE BORDER="1">
<TR>
<TD>Description: </TD>
<TD><TEXTAREA NAME="file_description" ROWS="10" COLS="50"></TEXTAREA></TD>
</TR>
<TR>
<TD>File: </TD>
<TD><INPUT TYPE="file" NAME="upload_file"></TD>
</TR>
<TR>
<TD COLSPAN="2"><INPUT TYPE="submit" VALUE="Upload"></TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
<?php
}
?>
Reply With Quote
  #2 (permalink)  
Old 11-17-04, 14:32
scottst scottst is offline
Registered User
 
Join Date: Nov 2004
Posts: 10
problem solved. it was due to a few carriage returns being inserted before the contents of the file.
Reply With Quote
  #3 (permalink)  
Old 12-03-04, 03:39
joshuayip joshuayip is offline
Registered User
 
Join Date: Apr 2003
Location: Malaysia
Posts: 1
Carriage return?

Can you show me how did you solve the problem?

Joshua
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On