Forum Moderators: coopster

Message Too Old, No Replies

Uploading and storing file and metadata

         

Awful newbie

9:23 pm on Apr 24, 2007 (gmt 0)

10+ Year Member



Hello, please be patient, but I really need a clue. I am using the following code to prosess and upload files as BLOBs into mysql. But I also need to store three additional inputs of metadata with each file. Do I only need to decalre some more post variables and insert them into the DB? Here is the processing code:

if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

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

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

//this is just mysql connection data
include 'library/config.php';
include 'library/opendb.php';

$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, query failed');
include 'library/closedb.php';

echo "<br>File $fileName uploaded<br>";

mcibor

8:50 am on Apr 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, you need to declare some more info on every file.

However in my opinion storing files as a blob is not a very good option.
I think better (faster) would be to store the path to the file with all relevant information and store the file in the filesystem. That's because most filesystems are optimized to deal with files.

But you will do as you wish.

Regards
Michal