Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word document in longblob

Status
Not open for further replies.

Bersani

Programmer
Nov 10, 2011
48
US
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.
 
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
 
I do not know the PHP code to make this happen.
 

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
 
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;
}
 
You have an error when you build your query with your double quotes:

Code:
$sql = [red]"[/red]INSERT INTO Files SET 
Title = [red]"[/red]" . 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
 
OK, if the output of the binary format does not look like a Word doc, what do you recommend?
 
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
 

<? 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;
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top