Everything seems to work great, except when it comes to displaying the image. Here is what I have:
This is the function I wrote to put images into the database. Some might notice the "mysql_insert_id()".. which seemingly defeats the purpose of using PEAR DB, but I am lazy and it works. I will fix that when/if I get this working.
Code: Select all
function add_image($article_id, $description, $file) {
$db = DB::connect(CMS_DSN);
if (DB::isError($db))
die ($db->getMessage());
if ($fileї'size'] > MAX_FILE_SIZE) {
return "ERROR: File is larger than max allowed.";
}
$sql = "INSERT INTO cms_images (article_id, description, filename, filesize, filetype)
VALUES ('" . $article_id . "', '" . $description . "', '" . $fileїname] . "', '" . $fileїsize] . "', '" . $fileїtype] . "')";
$result = $db->query($sql);
if (DB::isError($result))
die ($result->getMessage());
$id = mysql_insert_id($db->connection);
$fp = fopen($fileї'tmp_name'],"rb");
while (!feof($fp)){
$data = addslashes(fread($fp, MAX_SQL));
$sql = "INSERT INTO cms_imagedata (image_id, data)
VALUES ('" . $id . "', '" . $data . "')";
$result = $db->query($sql);
if (DB::isError($result))
die ($result->getMessage());
}
$db->disconnect();
}Code: Select all
$fetchmode = DB_FETCHMODE_ASSOC;
$images = array();
$article_id = $HTTP_GET_VARSї'id'];
$db = DB::connect(CMS_DSN);
if (DB::isError($db))
die ($db->getMessage());
$sql = "SELECT *
FROM cms_images
WHERE article_id = '" . $article_id . "'";
$result = $db->query($sql);
if (DB::isError($result))
die ($result->getMessage());
$i = 0;
while ($row = $result->fetchRow($fetchmode)) {
$imagesї$i] = $row;
$sql = "SELECT data
FROM cms_imagedata
WHERE image_id = '" . $rowї'image_id'] . "'
ORDER BY file_id DESC";
$result_data = $db->query($sql);
if (DB::isError($result_data))
die ($result_data->getMessage());
while ($row_data = $result_data->fetchRow($fetchmode)) {
$imagesї$i]ї'filedata'] .= stripslashes($row_dataї'data']);
}
$i++;
}
Header( "Content-type: image/pjpeg");
echo $imagesї0]ї'filedata'];
$db->disconnect();In a browser I would type "http://mysite/test.php?id=1" and I get the red X. So if you see something wrong with what I have, please let me know. I would greatly, greatly appreciate the help.