Uploading a Graphic to Database
Posted: Sat Sep 28, 2002 1:29 pm
How would i upload a graphic to the database and once it's on the database how do i link to it from a page?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
<?php
/*
Inserting
Here's the code for the form
<input type="file" name="image">
*/
mysql_connect("host","user","password");
mysql_select_db("db");
$image = $_POSTї'image'];
$sql = "INSERT INTO images VALUES('$image')";
mysql_query($sql);
?>Code: Select all
<?php
/*
Getting
*/
mysql_connect("host","user","password");
mysql_select_db("db");
$sql = "SELECT image FROM images WHERE id = $id";
$result = mysql_fetch_array(mysql_query($sql));
echo $result
?>I've tried to echo it to the screen and it printed the address to the file on my computer. does that mean that it didn't upload the actual file, just the path?To print the file you need the write to write it to a file then read it.
Code: Select all
if (isset($_FILESї'userfile']) && is_uploaded_file ($_FILESї'userfile']ї'tmp_name']))
{
/* <-- some mime_type checkings here --> */
// how to store the image type I leave up to you
$data = '';
/* reading the (binary) uploaded data */
$fd = fopen($_FILESї'userfile']ї'tmp_name'], 'rb');
while($nextpart = fread($fd, 4096)) // or some other
$data .= $nextpart; // tricky read-method
flcose($fd);
unlink($_FILESї'userfile']ї'tmp_name']);
/* request storage of blob data, assuming a autoindex-field (id) */
$query = "INSERT INTO images (imgData) VALUES ('".mysql_escape_string($data)."')";
mysql_query($query, $conn) or die('volka is dumb! '.mysql_error());
/* get the new autoindex-field's value */
print ('image uploaded, the img-id is:'. mysql_insert_id ($conn));
}Code: Select all
if ($_POSTї'imgid']) // or $_GET
{
// as mentioned the image-type-handling I leave up to you
header('Content-type: image/png');
$query ='SELECT imgData from images WHERE id='.(int)($_POSTї'imgid']);
$result = mysql_query($query, $conn) or die(error_log('imgData query failed'));
if ( ($row = mysql_fetch_row($result)) !== FALSE)
echo $rowї0];
else
echoErrorImage();
}Code: Select all
$queryG = "INSERT INTO Contest (`graphic`) VALUES ('". mysql_escape_string($data) ."'";
mysql_query($queryG, $connection) or die('volka is dumb! '.mysql_error());Code: Select all
if (isset($_FILESї'graphic']) && is_uploaded_file ($_FILESї'graphic']ї'tmp_name']))
{
// move_uploaded_file($_FILESї'userfile']ї'tmp_name'], "../../images/Contests/");
$data = '';
/* reading the (binary) uploaded data */
$fd = fopen($_FILESї'graphic']ї'tmp_name'], 'rb');
while($nextpart = fread($fd, 4096)) // or some other
$data .= $nextpart; // tricky read-method
fclose($fd);
unlink($_FILESї'graphic']ї'tmp_name']);
/* request storage of blob data, assuming a autoindex-field (id) */
$queryG = "INSERT INTO Contests (graphics) VALUES ('". mysql_escape_string($data) ."'";
mysql_query($queryG, $connection) or die('volka is dumb! '.mysql_error());
/* get the new autoindex-field's value */
print ("image uploaded, the img-id is:". mysql_insert_id ($connection) ."");
} else {
print ("image not uploaded.");
}Code: Select all
if ($_POSTї'imgid']) // or $_GET
{
// as mentioned the image-type-handling I leave up to you
header('Content-type: image/png');
$query ='SELECT imgData from images WHERE id='.(int)($_POSTї'imgid']);
$result = mysql_query($query, $conn) or die(error_log('imgData query failed'));
if ( ($row = mysql_fetch_row($result)) !== FALSE)
echo $rowї0];
else
echoErrorImage();
}Code: Select all
echo '<img src="contestImg.php?imgid=', $id, '" />'