How to insert image to DB and place in post?
It seems to be a trivial task but I just can't find a sample.
Say I have uploaded the image to temporary folder on my web server. Now I want to ins开发者_运维技巧ert this image to DB and place it to post.
Can someone provide a short snippet?
there are 2 ways to attach images to post:
- use post-related db table with BLOB field and then perform select for images + write
image.php?image_id
which will show actual image, this will restrict you to attach only images in your cms, but you can back-up your db with all images using phpmyadmin or so... - use post-related db table with column for image url (and store images to some folder), in this case you don't have to deal with
image.php?image_id
- apache will take care of this and on top of that you can attach any image with url (from other sites, etc.), not only uploaded ones
post-related table would be: id_image, id_post, [image_columns]
image_columns could include image title, date&time of last modification, and some other useful stuff...
You can store image to binary data fields (blob):
$handle = fopen("picture.jpg", "rb");
$img = mysql_real_escape_string(fread($handle, filesize('picture.jpg')));
fclose($handle);
$sql = "insert into pictures(id,img) values(null,'$img')";
This example shows how to display images stored in database :
http://www.anyexample.com/programming/php/php_mysql_example__image_gallery_%28blob_storage%29.xml
精彩评论