开发者

upload an image to mysql from android

THe only probl开发者_如何学Pythonem i am having is that when i add my image that is a jpeg, its type in my database is application / octet stream?

How do i make it stay as a jpeg in my database?


When handling uploads, it is generally not a good idea to store files directly in the database. Even though there is a type of field that can store such data, your database will quickly become large and slower to query.

When handling image uploads, your best bet is to keep the image as a file on the sever and store the path in the database. You'd use php's move_uploaded_file function (check out the doc), then put the $destination variable you used in move_uploaded_file into the database as just another piece of text.

Edit: If you still want to store the images in the database, I will remind you again that it is a bad, bad, bad idea, you will have to create a system that is dedicated to retrieving the data and outputting it as an image. For instance, this file would be called getimage.php:

<?php 
    @mysql_connect("localhost","root","") or
        die('Database error');
    @mysql_select_db("upload");

    $id = isset($_GET['image_id') ? mysql_real_escape_string($_GET['image_id']) : false;

    if ($id === false)
        die('Invalid image id');

    $q = @mysql_query("SELECT img_data, mime_type FROM table_name WHERE id = ".$id);
    $row = mysql_fetch_array($q);

    if (!row)
        die('Image not found');

    header("Content-type: ".$row['mime_type']);
    header("Content-Transfer-Encoding: binary");
    die($row['img_data']);
?>

You then use that as your image src:

<img src="getimage.php?image_id=1" />
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜