WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Upload image file to MySQL as BLOB
Categories : PHP, MySQL, Databases Update Picture
Anton Suryawan
Date : Jun 22nd 2003
Grade : 4 of 5 (graded 23 times)
Viewed : 35105
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Anton Suryawan
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

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
upload_max_filesize = 4M


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
A login page that require username, password and userlevel.
Categories : PHP, Security, Sessions, MySQL, Databases



This function will populate the options in a drop down HTML select list in a form from a database query.
Categories : MySQL, General SQL, PHP, HTML and PHP, Databases
PHP Transfer data from text file to Mysql Table
Categories : PHP, PHP Classes, Filesystem, Databases, MySQL
Logs hits to any page which includes it. Automatically utilises page access information left behind by PHP/FI2.0.
Categories : Databases, PHP, mSQL, Databases
Count how many weeks in the month have a specified day, such as Mon, Tue, etc. Var avail - number of days - first day name of the month, occurrences of Sun, occurrences of Mon, etc. Allows you to calculate number of working hours exclude Holidays.
Categories : Calendar, Date Time, PHP, Databases, MySQL
Linked comboboxes with php-mysql & javascript
Categories : PHP, Java Script, Databases, MySQL
Report Generation in Microsoft Access from a MYSQL database
Categories : PHP, MySQL, Databases, MS Access
Sort the results from a SELECT query (any number of columns) into an array automatically.
Categories : PHP, PHP Classes, Arrays, Databases, MySQL
Three ways to access data selected by a mysql query
Categories : MySQL, PHP, Databases
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
This script is a contact form between users of a website (kinda like the PM function on the forums)
Categories : PHP, Databases, MySQL, Regexps
html split bar used to split in multiple pages a database result
Categories : HTML and PHP, Databases, MySQL, PHP
 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 &gt;&gt;

&lt;form method="POST" action="&lt;?= $_SERVER["PHP_SELF"] ?&gt;" enctype="multipart/form-data"&gt;

fileopen &gt;&gt;

$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 );