Upload image file to MySQL as BLOB

This is a discussion on "Upload image file to MySQL as BLOB" within the PHP Forum section. This forum, and the thread "Upload image file to MySQL as BLOB are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Aug 16th, 2007, 17:33
Up'n'Coming Member
Join Date: Jul 2006
Location: manila
Age: 28
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Upload image file to MySQL as BLOB

hello,

i have this code below in which to upload image and store if in the database but it occurs an error upon running the program please help me im stack in here .. thankss

Code: Select all
<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(localhost,simply22,msiatech8) or exit("SERVER Unavailable");
  @mysql_select_db(simply22_simplyok,$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>

errors:

Warning:fopen: failed to open stream: No such file or directory in
Warning: feof(): supplied argument is not a valid stream resource in on line 26
Reply With Quote

  #2 (permalink)  
Old Aug 17th, 2007, 16:32
Up'n'Coming Member
Join Date: Feb 2006
Location: London
Age: 25
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Upload image file to MySQL as BLOB

your error message is basically saying that there is no image, so $_REQUEST["imgfile"] has no image 'in it'

remove your current enctype:
enctype="application/x-www-form-urlencoded"

if you want to upload any file with a form you must have this attribute:
enctype="multipart/form-data"

don't worry, I think this gets EVERY php developer when they're starting out...

that should do the trick

Last edited by jimz; Aug 17th, 2007 at 16:35. Reason: Forgot something
Reply With Quote
  #3 (permalink)  
Old Aug 21st, 2007, 14:44
Up'n'Coming Member
Join Date: Aug 2007
Location: Bicester
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Upload image file to MySQL as BLOB

Jim

Doesn't he need to use $_FILES rather than $_REQUEST to get the uploaded file ?

Justin
Reply With Quote
  #4 (permalink)  
Old Aug 22nd, 2007, 10:39
Up'n'Coming Member
Join Date: Feb 2006
Location: London
Age: 25
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Upload image file to MySQL as BLOB

I stand corrected! sorry...

Quote:
Since PHP 4.3.0, FILE information from $_FILES does not exist in $_REQUEST.
More info on request here: http://ca.php.net/manual/en/language...predefined.php

I don't use and would not advise using $_REQUEST at all and instead use $_GET or $_POST. I didn't want to confuse csun by giving to much info.
Reply With Quote
  #5 (permalink)  
Old Aug 22nd, 2007, 13:14
Up'n'Coming Member
Join Date: Aug 2007
Location: Bicester
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Upload image file to MySQL as BLOB

Hi Jimz

I didn't know it ever did.

Justin
Reply With Quote
Reply

Tags
mysql blob, upload

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP+SQL+File Upload joshlindem PHP Forum 5 May 10th, 2008 16:48
file upload berry05 Job Opportunities 0 Mar 19th, 2008 19:09
[SOLVED] Image Upload with PHP/MySQL kidreapertronV PHP Forum 5 Jan 31st, 2008 17:08
asp file upload script having problems with MYSQL paulmcn Classic ASP 2 Oct 5th, 2005 18:45
file upload asp-mysql database fluff Classic ASP 7 Aug 19th, 2004 10:45


All times are GMT. The time now is 20:53.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43