如何从我的表单上传图像到mysql数据库?

How I can upload image to mysql db from my form? In my form I set and in my databse I also defined data type for this fiels as longblob but not gettin image in to db

I m just simply inserting it into database with insert query.

Plz help me guys..

Use the blob data type for your column, read the uploaded file in and stripslashes. An example is below.

//open a file
$file = fopen($_FILES['fileToUpload']['tmp_name'], "r") or die("Error");

//read the contents of file
$contents = fread($file, filesize($_FILES['fileToUpload']['tmp_name']));

//add slashes. For details refer http://php.net/manual/en/function.addslashes.php
$contents = addslashes($contents);

//fire query to insert
mysql_query("INSERT INTO content(`img`) VALUES ('$contents');") or die(mysql_error()); 

//close file
fclose($file);

What you have to do is to store the path of the image that has been uploaded to specific folder you determined. Read the links below.

I think It's useless to copy and paste the code from the link.

http://staffweb.cms.gre.ac.uk/~mk05/web/PHP/imageUpload.html

http://www.namepros.com/webmaster-tutorials/81935-upload-image-to-mysql-database-php.html

http://www.namepros.com/webmaster-tutorials/81935-upload-image-to-mysql-database-php.html

If you have any issues let me know

Upload the file and store the files save path as a varchar

Upload the file to a folder in server using move_uploaded_file function

and save this path in your database field.

You can check following quick links for the working example of above.

  1. http://forum.codecall.net/topic/40286-tutorial-storing-images-in-mysql-with-php/
  2. http://www.tizag.com/phpT/fileupload.php
  3. http://www.techtoolblog.com/archives/upload-files-to-mysql-using-php-tutorial

check out them... you can use Upload.PHP for the same and then store the file in DB.

use blob data type field in database.

  $image_url = 'http://www.technew.in/templates/dino/images/technew_head.png';
  $imageData = chunk_split(base64_encode(file_get_contents($image_url)));
  $query = "INSERT INTO db_image (image_data) VALUES('$imageData')";
  mysql_query($query) or die(mysql_error());

This is better to to save your pictures in root of your project,such as images folder.
And save your path of pictures or name of pictures in your database with nvarchar or varchar data type.