Page 1 of 2

Uploading a Graphic to Database

Posted: Sat Sep 28, 2002 1:29 pm
by Zoram
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?

Posted: Sat Sep 28, 2002 2:34 pm
by Takuma
Ok...

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
?>
To print the file you need the write to write it to a file then read it.

Posted: Sat Sep 28, 2002 5:17 pm
by Zoram
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?
To print the file you need the write to write it to a file then read 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?

Posted: Sat Sep 28, 2002 7:30 pm
by volka
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.

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));
}
To get the image back from the db

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 snippets tested not even by compiler ;) )
(( &amp 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

Posted: Sat Sep 28, 2002 8:02 pm
by Zoram

Code: Select all

$queryG = "INSERT INTO Contest (`graphic`) VALUES ('". mysql_escape_string($data) ."'"; 
   mysql_query($queryG, $connection) or die('volka is dumb! '.mysql_error());
It returns 'volka is dumb! You have an error in your SQL syntax near '' at line 1'

any help?

Posted: Sat Sep 28, 2002 8:10 pm
by volka
in this special case I believe I'm not dumb, but you altered my code - so all (not granted) warranties are void ;)
try it with (`graphic`) --> (graphics)

Posted: Sat Sep 28, 2002 8:13 pm
by Zoram
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 :

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."); 
}
The field name from the form was graphic. Did i edit it wrong?

Posted: Sat Sep 28, 2002 8:35 pm
by volka
in $queryG a ) is missing.
($data) ."'"; --> ($data) ."')";

and please replace 'volka is dumb! ' by $queryG.': ' ;)

Posted: Sat Sep 28, 2002 8:41 pm
by Zoram
Alright, But i don't know if the

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(); 
}
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??

Posted: Sat Sep 28, 2002 8:48 pm
by volka
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

Posted: Sat Sep 28, 2002 8:50 pm
by Zoram
It's in the Contests table and there is one picture, it's a banner for the contest. Each Contest has it's own graphic.

Posted: Sat Sep 28, 2002 9:45 pm
by volka
when you display the other informations of the contest-entry place a

Code: Select all

echo '<img src="contestImg.php?imgid=', $id, '" />'
where 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

Posted: Sat Sep 28, 2002 10:12 pm
by Zoram
That doesn't make sense.

Posted: Sat Sep 28, 2002 10:23 pm
by volka
it does
so, two opinions. My statement is: I've seen such thing.
Yours: .....
;)



But I have to agree that the INSERT-statement is sensless in this context.
Do you store the image data in the same table as the other contest-infos?

Posted: Sat Sep 28, 2002 11:00 pm
by Zoram
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? :oops: