|
|
|
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> | | |
|
| bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, Databases | | | 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 | | | Cut your MySQL Connections to 1 line of code Categories : PHP, Beginner Guides, Databases, MySQL | | | A template script to provide the ability to get the next or previous n records from a MySQL database. Categories : Databases, PHP, MySQL | | | BBS system for easy customization. Utilizes mySQL. Categories : Complete Programs, MySQL, PHP, Databases | | | Broadcast HTML Email Categories : PHP, Email, MySQL, Databases | | | Returns the last row from a select where multiple rows have been selected.
In this case it is used to determine the last number in an AUTO_INCREMENT column. I use it to create "ticket numbers" or anything that needs a unique ID in sequence. Categories : mSQL, MySQL, Databases, PHP | | | MySQL Connection/Query Class Categories : Databases, MySQL, PHP, PHP Classes | | | Complex paging with no resultset limit Categories : PHP, MySQL, Databases, Output Control, HTML and PHP | | | Phorum, MySQL, Language, UK date format, MySQL UK Date format Categories : PHP, Date Time, Strings, MySQL, Databases | | | PHP Calendar Web App Categories : PHP, Databases, MySQL, Date Time, Calendar | | | How to load a query result into a PHP Array Categories : PHP, Databases, Arrays, MySQL | | | Functions for loading images into a MySQL database and displaying them. Categories : Graphics, HTML and PHP, MySQL, PHP, Databases | | | for each record, do this to the first record, and do that to any subsequent record Categories : PHP, Databases, MySQL, Beginner Guides | | | PHP4 AND MySQL Authentication Categories : PHP, MySQL, Authentication, Databases | |
| | | | 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 );
| |
|
|