downlaoding blobs?

A

Anonymous

Guest
is it possible to download a blob from the mysql database?
 
BLOB data fields are transferred in the same way as every other type of field. A SELECT will do just fine.
 
could you link an image to a blob? would they automatically download it?
 
back to first topic....when i selected a blob, it didn't do anything...
 
Virtuoso said:
back to first topic....when i selected a blob, it didn't do anything...

Why don't you give us a little background.. what are you storing in the field, what are you trying to do with it, what does your query look like, and what output did you get at the MySQL console?
 
i am storing a map for a game in the blob. I want people to be able to download it and save it onto their computer.
Code:
$map_query = "SELECT file FROM map WHERE id='$mapid'"

What do you mean by what output did i get at the mysql console?
 
Virtuoso said:
What do you mean by what output did i get at the mysql console?

I mean paste your query into the console and tell me what output you get. Always write your queries at the console (or in a tool like MySQLCC) before you use them in PHP. It will save you a lot of headaches.

Anyway, in order the make the user's browser know what sort of information it's getting (e.g. plain text, HTML, an image, video, etc.), you have to tell it. Using headers. Anyway, there's a dozen or two tutorials you could have googled in the time it took me to answer your question. Here's one:

http://www.phpbuilder.com/annotate/message.php3?id=1000550

Of course, it's almost universally a bad idea to store files in a MySQL database instead of just as, well, files. Access is slower and it takes up more space.
 
I did not really get the tutorial you posted but I found another one.

http://www.oreillynet.com/pub/a/370

When I try to download something, it goes to a new window and nothing happens.

Also this person's mysql_free_result($result); gives me an error.

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

if (isset($binFile) && $binFile != "none") {
$data = addslashes(fread(fopen($binFile, "r"), filesize($binFile)));
$strDescription = addslashes(nl2br($txtDescription));
$sql = "INSERT INTO tbl_Files ";
$sql .= "(description, bin_data, filename, filesize, filetype) ";
$sql .= "VALUES ('$strDescription', '$data', ";
$sql .= "'$binFile_name', '$binFile_size', '$binFile_type')";
$result = mysql_query($sql, $db);
mysql_free_result($result); // it's always nice to clean up!
echo "Thank you. The new file was successfully added to our database.<br><br>";
echo "<a href='main.php'>Continue</a>";
}
mysql_close();

} else {
?>
<HTML>
<BODY>
<FORM METHOD="post" ACTION="add.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="txtDescription" ROWS="10" COLS="50"></TEXTAREA></TD>
</TR>
<TR>
<TD>File: </TD>
<TD><INPUT TYPE="file" NAME="binFile"></TD>
</TR>
<TR>
<TD COLSPAN="2"><INPUT TYPE="submit" VALUE="Upload"></TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
<?php
}
?>

btw, it did not take me from the time of the last post to this post to find a tutorial. I kinda lost interest in PHP after I coldn't do any of the cool stuff i wanted to do.

Thanks, Virtuoso
 
Um, the code you posted is for uploading files into a database, not downloading them.
 
im asking two different things.

"mysql_free_result($result); " function gives me an error. Ill post it again this is the adding file:

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

if (isset($binFile) && $binFile != "none") {
$data = addslashes(fread(fopen($binFile, "r"), filesize($binFile)));
$strDescription = addslashes(nl2br($txtDescription));
$sql = "INSERT INTO tbl_Files ";
$sql .= "(description, bin_data, filename, filesize, filetype) ";
$sql .= "VALUES ('$strDescription', '$data', ";
$sql .= "'$binFile_name', '$binFile_size', '$binFile_type')";
$result = mysql_query($sql, $db);
mysql_free_result($result); // it's always nice to clean up!
echo "Thank you. The new file was successfully added to our database.<br><br>";
echo "<a href='main.php'>Continue</a>";
}
mysql_close();

} else {
?>
<HTML>
<BODY>
<FORM METHOD="post" ACTION="add.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="txtDescription" ROWS="10" COLS="50"></TEXTAREA></TD>
</TR>
<TR>
<TD>File: </TD>
<TD><INPUT TYPE="file" NAME="binFile"></TD>
</TR>
<TR>
<TD COLSPAN="2"><INPUT TYPE="submit" VALUE="Upload"></TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
<?php
}
?>


*edit Second quesion was solved. Turns out the id variables didn't have the same names
 
Virtuoso said:
"mysql_free_result($result); " function gives me an error.

It would be really nice if you told us what error it gives you. However, just reading the documentation gives us plenty of insight. Just read the very first post:

You can only use mysql_free_result with queries that return result sets. Calling mysql_free_result after a query that does not return a result set will generate an error:

PHP Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in ...

Well, the query that you posted doesn't return a result set -- why would you need to free any memory if there's no result set to take up memory?

Never us a function unless you know why you're using it.
 
Its tutorial though. A very undetailed one, but at least it works.

I still don't get why sometimes it opens the file instead of download them. Its like:

1) I click download
2) Asks me if I want to download
3) I click cancel
4) I click download again
5) Open up the file in the browser showing lots of weird characters
 
Back
Top