homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

need to "select the encoding" to read a document
attempting to retrieve blobs from mysql db

 8:15 pm on May 30, 2007 (gmt 0)

Hi everyone,

I am using this PHP code to upload word docs, rtf's, pdf's and text docs into a mysql db.

<form enctype="multipart/form-data" action="processuploadinsert.php" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="<? echo MAX_FILE_SIZE;?>" />
<!-- Name of input element determines name in $_FILES array -->
Upload this file: <input name="userfile" type="file" />
<input type="submit" value="Upload File" />

and the processuploadinsert file:

include 'dbconn.php';

if($_FILES['userfile']['size'] > 0)
$valuesAR['name'] = $_FILES['userfile']['name'];
$tmp_name = $_FILES['userfile']['tmp_name'];
$valuesAR['size'] = $_FILES['userfile']['size'];
$valuesAR['type'] = $_FILES['userfile']['type'];

$fp = fopen($tmp_name, 'r');
$content = fread($fp, filesize($tmp_name));
$valuesAR['content'] = addslashes($content);

if(!get_magic_quotes_gpc()) $fileName = addslashes($fileName);
$newID = processInsertArray($nodupefield,'upload',$uppercasefield,$valuesAR,$insertmsg);
if (!$newID) echo $insertmsg;
Table [upload]
uploadID int(11) (Auto) No
name varchar(75) Yes
type varchar(10) Yes
size varchar(10) Yes
content mediumblob Yes
else echo "<p>File $fileName uploaded with newID $newID</p>";

The uploads appear to be working correctly, as I'm getting the right filetype and filesizes inserted into the database.

Here is the code to download the blob from the database:

$thisID = $_REQUEST['id'];

include 'dbconn.php';

$row = getRow('upload',$thisID); //$row['fieldname']
$type = $row['type'];
$size = $row['size'];
$name = $row['name'];
$content = $row['content'];

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

echo $content;

Table [upload]
uploadID int(11) (Auto) No
name varchar(75) Yes
type varchar(10) Yes
size varchar(10) Yes
content mediumblob Yes


When I click on the link to call the download file, I am prompted to open or save the document, which is fine. Then, if it's a word or rtf document, I get a "File Conversion Window" box that wants me to "Select the encoding that makes your document readable."

My database field is mediumblob, and I didn't think I need to do anything other than that to upload binary. What am I missing? I've spent 2 days looking for an answer, and can't find anything.




 8:22 pm on May 30, 2007 (gmt 0)

forgot to add that text files are fine, but pdf's give the error message "there was an error opening this document. the file is damaged and could not be repaired"


 1:11 pm on May 31, 2007 (gmt 0)

this may not be the answer you're looking for but I never save files in the db. I usually save it to a dir somewhere and just store a path, and whatever else you need to know about the document, in th db.


 1:16 pm on May 31, 2007 (gmt 0)

//but I never save files in the db

Less worries about database size, less technical hassle, more db speed performance, . . .the list goes on.

It might differ from one case to the oter, but if you don't have a very good reason, files belong in directories. I agree with that response.



 1:27 pm on May 31, 2007 (gmt 0)

hi, yes i am aware of the reasons, benefits, drawbacks, etc. thanks for the advice, though. the documents need the extra security and portability that a database can provide.


 8:43 pm on Jun 1, 2007 (gmt 0)

Have you tried binary mode?

Windows offers a text-mode translation flag ('t') which will transparently translate \n to \r\n when working with the file. In contrast, you can also use 'b' to force binary mode, which will not translate your data. To use these flags, specify either 'b' or 't' as the last character of the mode parameter.

fopen [php.net]


 8:54 pm on Jun 1, 2007 (gmt 0)

hi coopster,

yes, i've since changed that line to:

$fp = fopen($tmp_name, 'rb');

but it still doesn't work.



 9:20 pm on Jun 1, 2007 (gmt 0)

The next step I would try then would be to turn around and instead of writing that data to the database, try pushing it back to the browser, preceding the $content with the correct header of course. By doing this, you will be able to determine for certain where the issue lies -- during the upload and reading of the file, or reading from the database.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved