Store Image in MySQL

How to store image in MySQL database via PHP?

Questions by kirangona

Showing Answers 1 - 10 of 10 Answers

gmanon

  • Feb 2nd, 2010
 

The best way to store images into MySQL is by storing the image location as a charater string.

If you need to manipulate the image, then, the best way is to copy the image as a bynary.

$iname=$_FILES['img1']['name'];
    $fp=fopen($_FILES['img1']['tmp_name'],r);
    $con=fread($fp,filesize($_FILES['img1']['tmp_name']));
       
    mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("db1");
    $con=addslashes($con);
    $sqlstt="insert into tbl_img values('$iname','$con')";
    if(mysql_query($sqlstt))
        echo "Image uploaded";
    else
        echo "Failed ";

  Was this answer useful?  Yes

abc

  • Sep 12th, 2011
 

Code
  1. $iname=$_FILES['img1']['name'];

  2.     $fp=fopen($_FILES['img1']['tmp_name'],r);

  3.     $con=fread($fp,filesize($_FILES['img1']['tmp_name']));

  4.        

  5.     mysql_connect("localhost","root","") or die(mysql_error());

  6.     mysql_select_db("db1");

  7.     $con=addslashes($con);

  8.     $sqlstt="insert into tbl_img values('$iname','$con')";

  9.     if(mysql_query($sqlstt))

  10.         echo "Image uploaded";

  11.     else

  12.         echo "Failed ";

  13.  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions