FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


uploading and retriving images from mysql





manav
hey ppl pls tell me a way for uploading and retriving images to and from mysql

pls provide the code....

thanks in advance....
roboguyspacedude
I don't think you can really do this. Personally I would upload the image using php to a file directory on your site and then save the url to that image in mysql. Maybe though since php can make an image in a string you could save it to mysql, but considering how long the string would be it wouldn't be practical to do it this way even if possible.
Star Wars Fanatic
You can do it using a blob.

Here is the code needed:

For the table:
Code:
CREATE TABLE Images (
PicNum int NOT NULL AUTO_INCREMENT PRIMARY KEY,
Image BLOB
);


For the upload file:
Code:
<?
If($Picture != "none") {
$PSize = filesize($Picture);
$mysqlPicture = addslashes(fread(fopen($Picture, "r"), $PSize));
mysql_connect($host,$username,$password) or die("Unable to connect to SQL server");
@mysql_select_db($db) or die("Unable to select database");
mysql_query("INSERT INTO Images (Image) VALUES ('$mysqlPicture')") or die("Can't Perform Query");
}
else {
echo"You did not upload any picture";
}
?>


Now for displaying the image:

Code:
<HTML>
<BODY>
<?
mysql_connect($host,$username,$password) or die("Unable to connect to SQL server");
@mysql_select_db($db) or die("Unable to select database");
$result=mysql_query("SELECT * FROM Images") or die("Can't Perform Query");
While($row=mysql_fetch_object($result)) {
echo "<IMG SRC=\"SecondType.php3?PicNum=$row->PicNum\">";
}
?>
</BODY>
</HTML>


And SecondType.php3:
Code:
<?
mysql_connect($host,$username,$password) or die("Unable to connect to SQL server");
@mysql_select_db($db) or die("Unable to select database");
$result=mysql_query("SELECT * FROM Images WHERE PicNum=$PicNum") or die("Can't perform Query");
$row=mysql_fetch_object($result);
Header( "Content-type: image/gif");
echo $row->Image;
?>



That's the basic code needed to get up and running in a few minutes.
If you want to know more, see this web page.
You can also find out more by searching Google for "saving images in mysql".

Hope it helps!
manav
hey thanks buddy

will give it a try and get back to u soon....
alem
i store the image as string in database.(used mysql data type: text)
it worked for ~20KB images.
this source says blob and text have same size 65535 characters.
but you can use for larger files MEDIUMBLOB with 16777215 characters and LONGBLOB 4294967295 characters.

but i am not an expert!
Related topics
Help Needed Regarding CGI And Php
Beginner..
Which image host do you use?
Phoenix free PHP scripts
Wolves
The Definative Web Hosting Guide
uploading system
Images...
A very good PHP MySQL Tutorial
Disk space needed
Images in MySQL
forum using bandwidth?
need advice on website format for non profit group
disallow uploading huge images
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.