Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Thank you again! I can't tell you how much I and my company appreciate what you've done! I love this place!..."

Geography

Where in the world do Tek-Tips members come from?
Bersani (Programmer)
10 Aug 12 14:38
I want to place a MS Word doc in longblob MySQL field. I want the Word doc to be in the table and not merely use the field as a placeholder for an external file location.
vacunita (Programmer)
10 Aug 12 15:07
hmm, O.k. that's good. What's the problem?

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

Bersani (Programmer)
10 Aug 12 15:45
I do not know the PHP code to make this happen.
vacunita (Programmer)
10 Aug 12 16:16

From the MYSQL side it would be as simple as

CODE

UPDATE tablename SET blobfieldname = 'blobcontents' WHERE rowId='someid'; 

From he PHP side you'll basically want to open the file, using fopen, or even just the file() function, and then simply assign the contents to a variable you can use in your query.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

Bersani (Programmer)
10 Aug 12 16:42
I have tried this code but it does not retrieve the word doc
// Slurp the content of the file into a variable

$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc()){
$fileName = addslashes($fileName);
}

$file_info = pathinfo($_FILES['file']['name']);

$sql = "INSERT INTO Files SET
Title = "".htmlentities(stripslashes($_POST['title']))."",
File_Name = '".$fileName."',
File_Type = '".$fileType."',
File_Size = '".$fileSize."',
File_Content = '".$content."',
File_Extension = '".$file_info['extension']."'";


$result = mysql_query($sql);

// If the query was successful, give success message

if(!$result){
echo "Could not add this file.";
exit;
}
else{
echo "New file successfully added.";
}

}else{
echo "Invalid file.";
exit;
}
vacunita (Programmer)
10 Aug 12 17:47
You have an error when you build your query with your double quotes:

CODE

$sql = "INSERT INTO Files SET 
Title = "" . htmlentities(stripslashes($_POST['title']))."", 
File_Name = '".$fileName."', 
File_Type = '".$fileType."',
File_Size = '".$fileSize."',
File_Content = '".$content."',
File_Extension = '".$file_info['extension']."'"; 

Which should have shown up if you have display_errors on in your PHP installation, which for development you should always have on.
Try:

CODE

$sql = "INSERT INTO Files SET 
Title = '".htmlentities(stripslashes($_POST['title']))."', 
File_Name = '".$fileName."', 
File_Type = '".$fileType."',
File_Size = '".$fileSize."',
File_Content = '".$content."',
File_Extension = '".$file_info['extension']."'"; 

Other than that, echoing out $content should tell you if the file is being read or not.
Since the Word format is a binary format, it will have many special characters, besides the actual text, so it may not look like a Word document when its echoed out RAW.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

Bersani (Programmer)
10 Aug 12 18:20
OK, if the output of the binary format does not look like a Word doc, what do you recommend?
vacunita (Programmer)
10 Aug 12 19:07
For output?

Just issue the appropriate headers, when sending back the blob data.

CODE

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $file_name);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size_of_file);
//read file from physical path
readfile($download_path); 

This should force the download for the file.

Alternatively:

CODE

header('Content-Description: File Transfer');
header('Content-Type: application/vnd.msword');
... 

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

Bersani (Programmer)
10 Aug 12 22:44

<? php
$dbh = mysql_connect("localhost","user_name","password") or die("There was a problem with the database connection.");
$dbs = mysql_select_db("MyDatabase", $dbh) or die("There was a problem selecting the categories.");

// Now, We'll check to make sure our $_GET variable is a number

if(!is_numeric($_GET['id']){
echo "Invalid file chosen.";
exit;
}



$sql = "SELECT * FROM Files WHERE Files.ID = ".$_GET['id'];

$result = mysql_query($sql);

// If the query was invalid or failed to return a result, an error is thrown

if(!$result || !mysql_num_rows($result)){
echo "Invalid file chosen.";
exit;
}


$curr_file = mysql_fetch_assoc($result);

$size = $curr_file['File_Size'];
$type = $curr_file['File_Type'];
$name = $curr_file['File_Name'];
$content = $curr_file['File_Content'];

header("Content-length: ".$size."");
header("Content-type: ".$type."");
header('Content-Disposition: attachment; filename="'.$name.'"');
echo $content;
?>

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close