0

I have a need to store an image which is preloaded in a form in a blob filed in MySQL database.

Here is the form code:

<form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td>Image Name</td>
      <td><input type="text" name="imgName" id="imgName" /></td>
    </tr>
    <tr>
      <td>Image</td>
      <td><input type="image" name="myImg" id="myImg" src="images/1209894_11404408.jpg" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td colspan="2" align="center" valign="middle"><input type="submit" name="Submit" id="Submit" value="Submit" /></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1" />
</form>

Here is the table structure create statement. I am using InnoDB:

CREATE TABLE IF NOT EXISTS `img` (
  `img_id` int(11) NOT NULL AUTO_INCREMENT,
  `img_name` varchar(200) DEFAULT NULL,
  `img_img` longblob,
  PRIMARY KEY (`img_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

The Inser Query that I am using is:

INSERT INTO img (img_name, img_img) VALUES ('$_POST['imgName']', '$_POST['myImg']')

TIA

Yogi Yang

Yogi Yang 007
  • 5,147
  • 10
  • 56
  • 77

2 Answers2

1

first of all you need to set up your html form to support file uploads, the correct form tag to allow that will be:

<form enctype="multipart/form-data" id="form1" name="form1" action="<?php echo $editFormAction; ?>" method="post">

And the PHP script to process the upload form will be something like:

if (isset($_FILES['myImg']) && $_FILES['myImg']['size'] > 0) {
  $tmpName = $_FILES['myImg']['tmp_name'];
  $fp = fopen($tmpName, 'r');
  $data = fread($fp, filesize($tmpName));
  $data = addslashes($data);
  fclose($fp);

  //Fill all the other form vars

  $query = "INSERT INTO img ";
  $query .= "(img_img) VALUES ('$data')";
  $results = mysql_query($query, $link);
 }

Of course you need first to open you MySQL connection, and close it after completion.

Hope it helps.

Kind regards

jandro_es
  • 44
  • 2
  • If you read my original post I have specifically said the image is pre loaded in the form in input control () so there is no question of uploading an image file to server then. I know that as there are many solution on net showing the method that you have suggested here. But my requirement is very different. Let me rephrase my requirements. I want to save the image loaded in input filed whose type is 'image' to a file. So I want to save the content of image as shown in browser to a file on server. Thx. – Yogi Yang 007 Mar 30 '13 at 04:21
0

Finally I have found the solution.

Here is the code that I had to use to save images to MySQL properly.

INSERT INTO img (img_name, img_img) VALUES ('$_POST['imgName']', base64_decode(substr('$_POST['myImg']', 22)));

In the same way if we want to save the file to server we can use following code in PHP

$myid = fopen($filesavepath.$imgfilename,'wb');
$svgimg=$_REQUEST['svgimg'];
fputs($myid, base64_decode(substr($_POST['myImg'], 22)));
fclose($myid);

Hope this helps someone.

Yogi Yang

Yogi Yang 007
  • 5,147
  • 10
  • 56
  • 77