Forums

This topic is locked

Please - help with MySQL blobs

Posted 23 years ago
1
has voted
23 years ago Michael Davis posted:
Please excuse me if this is a basic question, but I'm new to this. I'm using a MySql/PHP combination with the ImpAkt extention. I'm using user authentication to restrict users to a set of web pages, but I also have a bunch of PDF files for these users.
My questions:
1: Should these PDF's be blob files in the database? I don't want people to be able to type "www.mysite.com/pdffile.pdf" and get the file.
2: How do I load a pdf/binary file into a blob field? I'm not sure how to do this.

TIA for any help!

Replies

Replied 23 years ago
23 years ago Tim Green replied:
BLOBS have to be treated differently than other database items, as they are by nature entire files. Certainly if you don't want these PDF's to be accessed in the way you mentioned, a blob is a really good method to store this information, provided you have plenty of space on your MySQL server.

First of all, place all of your PDF's into a single directory on your webserver (don't worry you can delete them when we're done).

That done, put the following script into a PHP page (presuming your using PHP. If not then one of the other developers will have to help you).

<?php
$dirPath = "relative/path/to/files";
$handle=opendir($dirPath);
while (false!==($file = readdir($handle))) {
if ($file != "." && $file != ".." {
writeToBlob($file, $dirPath);
}
}
closedir($handle);

function writeToBlob($file, $dirPath) {

// Read File
$newHandle = fopen($dirPath . "/" . $file, "r";
$contents = fread ($fd, filesize ($filename));
fclose($newHandle);

// Write it to the DB
$theSQL = "INSERT INTO tablename (blobcolumn) values ('".$contents."')";
$theRS = $theConnection->Execute($theSQL) or DIE($theConnection->ErrorMsg());
$theRS->Close();

}

?>


Remember to replace variable names and parameters accordingly. If other data is being written to the Table before this happens then you will need to change the SQL to an UPDATE rather than an INSERT.

Hope this helps.

Tim Green
tim@udzone.com
Extension & PHP TalkZone Manager
<font size=1>-------------------------------------------
<i>Please read the Forum FAQ before posting
a question to this TalkZone.</i>
-------------------------------------------
www.UDzone.com : A dynamic Dreamweaver,
Ultradev and Fireworks site for developers
by developers.
-------------------------------------------</font id=size1>

Reply to this topic