WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
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 Resources
Web Development Content
Internet Security Software
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
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

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 29 times)
Viewed : 77471
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  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
This class splits the results of the query into multiple pages like what the search engine does.
Categories : PHP Classes, PHP, MySQL, Databases
A Simple sign up script with PHP and JavaScript validations.
Categories : PHP, Java Script, MySQL, Databases
Download manager - A PHP script for adding a download page to any site.It also enables you track the no. of downloads.
Categories : PHP, Content Management, Filesystem, Databases, MySQL
Checks Date-Input from HTML-Forms and converts to YYYY-MM-DD Format for MySQL Date-Fields
Categories : MySQL, Date Time, PHP, Databases
Create and restore backup of MySQL databases
Categories : MySQL, Databases, PHP, PHP Classes, Complete Programs
How to thread a list of messages in database and show it in a treelike structure
Categories : PHP, MySQL, Databases
Message of the Day - Random Message (Needs MySQL!)
Categories : Databases, HTML and PHP, PHP, MySQL
Required form fields that pull from MySQL database
Categories : PHP, HTML and PHP, Databases, MySQL
Ajax PHP Tree (Left and Right) with MySQL
Categories : PHP, Databases, MySQL, AJAX, PHP Classes
YellowPages Content Grabber (PHP5 +)
Categories : PHP, PHP Classes, Regexps, Databases, MySQL
List people whose birthdays fall on the current Day and Month
Categories : Databases, Date Time, MySQL, PHP
Newbie Notes #4 - Trapping dumb MySQL query errors
Categories : PHP, Databases, MySQL, Debugging, Beginner Guides
email new items in db
Categories : PHP, Email, Databases, MySQL, Beginner Guides
 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 );