[PHP-DB] Inserting Image as Blob in MySql DB

2001-03-25 Thread Phil Jackson

Hi,
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 "brbrcenter";
echo "img src='$thisfile'";
$crapola = "101010101";
echo "brbrcenter";



 $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 J.

.


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP-DB] Inserting Image as Blob in MySql DB

2001-03-25 Thread Stuart J. Browne

 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 "brbrcenter";
 echo "img src='$thisfile'";
 $crapola = "101010101";
 echo "brbrcenter";



  $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



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]