Reputation: 1
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
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
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
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
Reputation: 5977
You can check following quick links for the working example of above.
check out them... you can use Upload.PHP for the same and then store the file in DB.
Upvotes: 0
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
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