Re: Inserting Image as Blob in MySql DB

From: Date: Mon, 26 Mar 2001 06:37:36 +0000
Subject: Re: Inserting Image as Blob in MySql DB
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-7795@lists.php.net to get a copy of this message
> Does anyone know the proper method for inserting an image(*.jpg or > *.gif) into a MySql database blob field? I am recieving the file via an > html form with the usual enctype for this and <input type=file> > parameters. I can save it as just a regular file and then display it > fine on the page, but no way can I update the database with this...and > have tried everything imaginable...surely this is done everyday and is > quite simple. > > code snippet below - and yes, I am a php beginner...ASP by day and > trying to learn > PHP by night.. > ------------------------- > $photo = fopen($txtImage, "r"); > $filecontents = fread($photo, filesize($txtImage)); > echo "<br>"; > echo "<img src='$photo'>"; > $anotherfile = fopen("/www/htdocs/Survey/$thisfile", "a+"); > fputs($anotherfile, $filecontents); > //fclose($anotherfile); > echo "<br>"; > echo "<img src='$anotherfile'>"; > echo "<br>"; > echo $photo; > echo "<br><br><center>"; > echo "<img src='$thisfile'>"; > $crapola = "101010101"; > echo "<br><br><center>"; > > > > $connection = > mysql_connect("mysql.iticom.net","fractalv","fw348vnj") > or die ("crap!"); > $db = mysql_select_db("fractalv", $connection) or die ("db"); > $sql = "INSERT INTO Images (Image, > ImageType,ImageGallery,ImageLink,ThumbnailLink) VALUES > ('$filecontents','$txtImageType', '$txtImageGallery', > '$txtImageLink', > '$txtThumbnailLink')"; > $sql_result = mysql_query($sql,$connection) or die ("could not insert > into DB"); > mysql_close($connection); > ---------------------ect. > > thanks in advance for absolutley any ideas! Phil, I can think of two possible ways.. Depending on the version of MySQL you have. If you have v3.23, MySQL has a function called LOAD_FILE() which you can pass the temporary file-name that you get from the wb page: mysql_query( "insert into Images ( Image ) values ( load_file( $txtImage ) );" ); Failing that, I would think that you have to read it in (as you do above), escape it (addslashes() in php) then isnert that escaped string into the database : mysql_query( "insert into Images ( Image ) values ( '" . addslashes( $filecontents ) . "' ); " ); Anyway.. a few possibilities to look into. Bkx

« previous php.db (#7795) next »