0

I have searched a lot on about this but I did not find solution.

I have images in a directory in server. And I want to upload those images ( Not Just image Paths) to MySQL database using longblob with PHP.

I know that storing images in the database is not recommended but that is the requirement of my project so I want use this method.

Please suggest me, How can I do that? Thanks to all.

Maria
  • 149
  • 1
  • 3
  • 11
  • http://stackoverflow.com/questions/17717506/how-to-upload-images-into-mysql-database-using-php-code – user1844933 Feb 01 '14 at 12:45
  • Nowadays I see no reason for not storing blobs like images, pdfs or whatever in to a database directly. Could have been true 5 years ago, but space isn't an issue any more as well as cpu power isn't. – Axel Amthor Feb 01 '14 at 12:48

1 Answers1

0
<?php
$con = mysql_connect("localhost","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());

}
mysql_select_db("your databse name");

// Make sure the user actually
// selected and uploaded a file
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {

// Temporary file name stored on the server
$tmpName = $_FILES['image']['tmp_name'];
$name=$_POST['name'];
// Read the file
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);


// Create the query and insert
// into our database.
$query = "INSERT INTO image2 ";
$query .= "(image,name) VALUES ('$data','$name')";
$results = mysql_query($query, $con);

// Print results
print "Thank you, your file has been uploaded.";

}
else {
print "No image selected/uploaded";
}

// Close our MySQL Link
mysql_close($con);
?>
Abhishek kadadi
  • 120
  • 2
  • 11