Uploading a Graphic to Database
Moderator: General Moderators
Uploading a Graphic to Database
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?
Ok...
To print the file you need the write to write it to a file then read it.
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
?>Does that mean that to view it in a page you could echo it and it would work? Or do you have to do something else to view it?
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.
if you want to store the image data (instead of the local file name) you should to add enctype="multipart/form-data" method="post" to your form and have to read the file data from $_FILES['userfile']['tmp_name'] where userfile is the name of the input (file) field in your form (see Chapter 20. Handling file uploads).
Store the data in a blob-field.
To get the image back from the db(code snippets tested not even by compiler
)
(( & in php-block -> & ))
some manual-entries:
is_file_uploaded
fread
maybe readfile and ob_start
mysql_escape_string
mysql_query
error_log
mysql_insert_id
Store the data in a blob-field.
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();
}(( & in php-block -> & ))
some manual-entries:
is_file_uploaded
fread
maybe readfile and ob_start
mysql_escape_string
mysql_query
error_log
mysql_insert_id
Code: Select all
$queryG = "INSERT INTO Contest (`graphic`) VALUES ('". mysql_escape_string($data) ."'";
mysql_query($queryG, $connection) or die('volka is dumb! '.mysql_error());any help?
What was the '(imgData)' from? wasn't it just the col name for the field in the table?
Here is what i have changed it to :
The field name from the form was graphic. Did i edit it wrong?
Here is what i have changed it to :
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.");
}Alright, But i don't know if the Will work for what i'm doing. If i want them to upload a jpg or a gif, how do i print the graphic to the page, not the coding for it??
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();
}depends on the criteria by which the image is selected.
you named the table 'Contests', so you store contender pictures or awards or ... or ...?
is it a simple 1:1 relation (i.e one contender one picture) or a 1:m relation (i.e. one contender multiple award pictures) ?
maybe http://forums.devnetwork.net/viewtopic.php?t=2983 is of use for you
you named the table 'Contests', so you store contender pictures or awards or ... or ...?
is it a simple 1:1 relation (i.e one contender one picture) or a 1:m relation (i.e. one contender multiple award pictures) ?
maybe http://forums.devnetwork.net/viewtopic.php?t=2983 is of use for you
when you display the other informations of the contest-entry place awhere you want the image to be shown, where $id is the unique identifying field's value for this contest in your table.
Replace if ($_POST['imgid']) by if ($_GET['imgid']) in contestImg.php
Code: Select all
echo '<img src="contestImg.php?imgid=', $id, '" />'Replace if ($_POST['imgid']) by if ($_GET['imgid']) in contestImg.php
Yes, i do, I have the image in the same table as the rest of the contest information. All i want to do now that i have it so that i can upload the graphic to the server is to have it output it in the page... but i can't make heads or tales of it... I can't figure out how to make it output the graphic as a graphic. could you script something that would just output the graphic from the Contests table? 