user1899786
user1899786

Reputation: 1

How I can upload image to mysql db from my form?

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..

Upvotes: 0

Views: 175

Answers (7)

Brian Hannay
Brian Hannay

Reputation: 600

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);

Upvotes: 1

Hamid Talebi
Hamid Talebi

Reputation: 1318

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.

Upvotes: 0

Dino Babu
Dino Babu

Reputation: 5809

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());

Upvotes: 1

Jigar Pandya
Jigar Pandya

Reputation: 5977

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.

Upvotes: 0

Prasanth Bendra
Prasanth Bendra

Reputation: 32730

Upload the file to a folder in server using move_uploaded_file function

and save this path in your database field.

Upvotes: 0

Hydra IO
Hydra IO

Reputation: 1557

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

Upvotes: 0

Techie
Techie

Reputation: 45124

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

Upvotes: 0

Related Questions