开发者

MYSQL works with Images?

How to stor开发者_StackOverflowe multiple images in a MySQL database and call them back whenever need? I need to store multiple data of people and their pictures for profile functioning on my website. Please help.

UPDATED QUESTION

P. S. - I KNOW THERE IS A SOLUTION TO STORE THE FILE ON THE FILESYSTEM. BUT I WANT TO KNOW THE PROCESS OF STORING IT ON DB. SO PLEASE HELP ME INSTEAD OF GIVING ABRUPT ANSWERS.


I also will answer that is not a good idea to do so...

by the way you can do it using BLOB column type and in the upload function you have to insert it into the query as $_FILES['uploaded_file']['name']

as example: INSERT INTO db_table (id, image) VALUES (NULL, '{$_FILES['userfile']['name']}');


You can use the blob type to store binary data but i would strongly recommend against that.

For performance reasons you should store the images in a data directory and then save the path in the database.

This will allow you to use php's build in file outputting methods (readfile) as well as x-sendfile.

If you need to load the image from the database then you'd be loading the image in php' memory for each request.

But if you really want to do it,

<?php
saving the file would be something like this
$file = $_FILES['uploadedfile']['tmp_name'];
$data = addslashes(fread(fopen($file, "r"), filesize($file)));

mysql_query("INSERT INTO files (file) VALUES ('$data')");

just fetch the data from the row and then do something like:

 <?php
 $res = mysql_query("select file FROM my_files WHERE file_id=$file_id");
 list($data) = mysql_fetch_row($res);
 header("Content-type: $type");
 header("Content-length: $size");
 header("Content-Disposition: attachment; filename=$name");
 header("Content-Description: PHP Generated Data");
 echo $data;

code above is not complete, would need to be cleaned up, have securty checks added, and other stuff that would would want in the table. just a qucik example


use the BLOB datatype.

BLOB stands for Binary Large OBject, and it can be used to store any kind of binary file including images.

Hope this helps.

Andy


I think that you shouldn't store de images in the database. In a web environment, store the images in your server hard disk (or in a external service like amazon S3 or another solution). In the database you only need to store a reference to this images.

For example, if you have a web in /home/web/ you can create a directory like /home/web/images/. A better practice is create different directories like /home/web/images/2011/03/ because sotore too many files in one directory is not a good idea.

Then, you can store this paths in your user table: field image: varchar(XXX) = '/home/web/images/2011/03/image-for-one-user.jpg'

When you write de image in your HTML:

<img src="/images/2011/03/image-for-one-user.jpg" />

The answer for your question is: You can store binary data in mysql in a blob field. But I don't like it. think, if you have multiple users, you will have a very large database.


This seems to me quite overkill and not efficient, and I've never heard of anyone storing files directly in a DB.. But then again, I'm note that experienced.

Can't you go with storing your pictures somewhere on your server, and storing the path on the DB ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜