|
|
|
a tutorial to upload an image file to MySQL as BLOB data.
u need 3 files:
- test_imagedb.php = form to upload
- test_imagedb_create.php = retrieve image from db
- test_imagedb_view.php = yeah.. view it.. what else? :D
but before we continue, change your setting on "php.ini" and "my.ini" to accept large image files.
php.ini
my.ini
| [mysqld]
set-variable=key_buffer=16M
set-variable=max_allowed_packet=16M | |
set those directives above to any value that you want. now, we can continue..
mysql dump
| [code]
#
# Table structure for table 'tblimage'
#
CREATE TABLE `tblimage` (
`imgid` int(3) unsigned NOT NULL auto_increment,
`imgtype` varchar(16) NOT NULL default '',
`imgdata` mediumblob,
PRIMARY KEY (`imgid`)
) TYPE=MyISAM; | |
test_imagedb.php
| [code]
<body>
<?
if (!isset($_REQUEST["submit"])) {
?>
<form method="POST" action="<?= $_SERVER["PHP_SELF"] ?>" enctype="application/x-www-form-
urlencoded">
<table>
<tr><td>Type</td><td><select name="imgtype"><option value="image/gif">GIF</option><option
value="image/jpeg">JPEG</option></select></td></tr>
<tr><td>File</td><td><input type="file" name="imgfile"></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="upload"><input type="reset"></td></tr>
</table>
</form>
<?
//-- save image to db --
} else {
/*
the code below is a suggestion from California Strong...
*/
$hndl=fopen($_REQUEST["imgfile"],"r");
$isize=sizeof($_REQUEST["imgfile"]);
$imgdata="";
while(!feof($hndl)){
$imgdata.=fread($hndl,$isize);
};
/*
my code was...
$hndl=fopen($_REQUEST["imgfile"],"r");
$imgdata=fread($hndl,filesize($_REQUEST["imgfile"]));
*/
$imgdata=addslashes($imgdata);
$dbconn = @mysql_connect($dbserver,$dbuser,$dbpass) or exit("SERVER Unavailable");
@mysql_select_db($dbname,$dbconn) or exit("DB Unavailable");
$sql = "INSERT INTO tblimage VALUES(NULL,'". $_REQUEST["imgtype"] ."','". $imgdata ."')";
@mysql_query($sql,$dbconn) or exit("QUERY FAILED!");
mysql_close($dbconn);
fclose($hndl);
echo "<a href=\"test_imagedb_view.php\">view image</a>";
};
?>
</body> | |
test_imagedb_create.php
| [code]
<?
$dbconn = @mysql_connect($dbserver,$dbuser,$dbpass) or exit("SERVER Unavailable");
@mysql_select_db($dbname,$dbconn) or exit("DB Unavailable");
$sql = "SELECT imgtype,imgdata FROM tblimage WHERE imgid=". $_GET["imgid"];
$result = @mysql_query($sql,$dbconn) or exit("QUERY FAILED!");
$contenttype = @mysql_result($result,0,"imgtype");
$image = @mysql_result($result,0,"imgdata");
header("Content-type: $contenttype");
echo $image;
mysql_close($dbconn);
?> | |
test_imagedb_view.php
| <body>
<?
$dbconn = @mysql_connect($dbserver,$dbuser,$dbpass) or exit("SERVER Unavailable");
@mysql_select_db($dbname,$dbconn) or exit("DB Unavailable");
$sql = "SELECT imgid,imgtype FROM tblimage ORDER BY imgid";
$result = @mysql_query($sql,$dbconn) or exit("QUERY FAILED!");
echo "<table border=1>\n";
echo "<tr><th>imgid</th><th>imgtype</th><th>imgdata</th></tr>\n";
while ($rs=mysql_fetch_array($result)) {
echo "<tr><td>".$rs[0]."</td>";
echo "<td>".$rs[1]."</td>";
echo "<td><img src=\"test_imagedb_create.php?imgid=".$rs[0]."\"></td></tr>\n";
};
echo "</table>\n";
mysql_close($dbconn);
?>
</body> | | |
|
| This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. Categories : Databases, MySQL, Complete Programs, PHP, Databases | | | bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, Databases | | | Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs. Categories : Databases, PHP, MySQL, Complete Programs | | | phpAds, a complete banner and ad management system with detailled tracking and stats. Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases | | | Point and Click Interface ala MS Access for creating SQL statements. Categories : MySQL, Complete Programs, General SQL, PHP, Databases | | | Message of the Day - Random Message (Needs MySQL!) Categories : Databases, HTML and PHP, PHP, MySQL | | | email new items in db Categories : PHP, Email, Databases, MySQL, Beginner Guides | | | Alternating background color for HTML table rows Categories : PHP, Databases, MySQL, HTML and PHP | | | color codes for positive and negative numbers Categories : PHP, MySQL, Databases, HTML | | | Authorize Me! An authentication script. Categories : MySQL, Databases, Authentication, PHP | | | A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL | | | Linked comboboxes with php-mysql & javascript Categories : PHP, Java Script, Databases, MySQL | | | mysql_escape_string Categories : PHP, MySQL, Databases, Strings | | | Automatically printing the contents of an sql table in MySQL. Categories : MySQL, PHP, HTML and PHP, Databases | | | usercounter class Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables | |
| | | | Victor Cao wrote : 981
How about a multiple image upload? Say for a photo album?
-php newb
| | | | Brian Tully wrote : 982
thanks! just what i was looking for :)
| | | | Strong California wrote : 996
hi~thank for share the code.but i find a bug in your code.because it can`t upload the larger image file.
$imgdata=fread($hndl,filesize($_REQUEST["imgfile"]));
when this line want to read from image file,the FROM is complete posted. so fread read end of the image file.
try to use this:
$imgdata=``:
while(!feof($hndl)){
$imgdata.=fread($hndl,2048);
}
to fix the BUG!
| | | | Javier AR wrote : 1006
Code is great, i worked out fine, just one question:
do I have to upload the image file directly from the
server`s directory=?? I tried to upload an image from my
hard disk and it didn`t work, when I uploaded an img
located in the server it worked just fine and the image
was saved in the blob field...., Tnx in advance!
| | | | MIke Iacono wrote : 1021
I have tried this example and others like it. I know that
the data is a jpg and is a blob however when I try the
script I get a long line of numbers and letters. If I
could use a folders and relative file names I would
however I am synching two databases and am at the mercy of
the maintainer of the others database.
Any help would be appreciated.
Thanks,
Mike
| | | | gio dex wrote : 1028
In localhost with windows xp works fine but in remote on server linux it doesn`t work.
But I`ve done some modify and now works fine on my linux server.
I`ve changed:
enctype >>
<form method="POST" action="<?= $_SERVER["PHP_SELF"] ?>" enctype="multipart/form-data">
fileopen >>
$hndl=fopen($imgfile,"r") or die ("I CANT OPEN $imgfile");
I don`t know if this is right thing but it works!
Bye
giodex
| | | | wrote : 1802
how would one go about uploading an image and have the
original keep the same size yet output a clickable
thumbnail and additionally show some sort of text
description
| | | | Eduardo Moreira wrote :1807
In my server your code didnt work, i dont know why but a used this code to correct the problem:
$imgdata = file_get_contents($_FILES['imgfile']['tmp_name']);
$imgdata = mysql_real_escape_string($imgdata );
| |
|
|
|