08-16-2009, 03:22 PM
|
#1 (permalink)
|
|
The Visitor
Join Date: Aug 2009
Posts: 2
Thanks: 0
|
Need help getting info into database
I have a file that is reading the files in a directory. I also want the file to upload the data to the database table. I can get it to send one file (even though its wrong) to the database. I am sure its a problem with my variables but am I even going about this the correct way ? I also just thought about this part but if the files already exist in the DB then i do not want them written again. Heres what I have so far.
Code:
<?php
$path = "./";
$files = "*.gif";
$name = '$filename';
$size = 'size';
$type = 'type';
$sfiles = glob($path.$files);
asort($sfiles);
foreach ($sfiles as $filename) {
echo "<br>\n", "$filename " . filesize($filename);
}
$content = file_get_contents($filename);
if ($conn = mysqli_connect('localhost', 'root', 'xxx', 'xxx')) {
$content = mysqli_real_escape_string($conn, $content);
$sql = "insert into images (name, size, type, content) values ('{$name}', '{$size}', '{$type}', '{$content}')";
if (mysqli_query($conn, $sql)) {
$uploadOk = true;
$imageId = mysqli_insert_id($conn);
} else {
echo "Error: Could not save the data to mysql database. Please try again.";
}
mysqli_close($conn);
} else {
echo "Error: Could not connect to mysql database. Please try again.";
}
?>
The table strcture is
Name
Size
Type
Content <- mediumblob binary (not sure if that is correct either)
|
|
|
|