php中的字符串到图像转换并存储在mysql数据库中

时间:2015-02-27 12:20:06

标签: php android mysql string image

我想将一个图像(由android开发人员传递)存储到mysql数据库中。我在PHP工作。 Plz帮助我编写php代码的脚本。

Actualy我问了一半问题。主要问题是如何在上传文件夹中存储图像?

3 个答案:

答案 0 :(得分:0)

使用

file_get_contents($file)

这将返回您可以在mysql DB中存储为TEXT的字符串内容。

答案 1 :(得分:0)

如果图像是使用POST表单if you use DefaultHttpClient on Android side发送的,则可以尝试此操作:

<?php
// Database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);

// Prepare the query
$query = $conn->prepare(
    'INSERT INTO images_table (id, image_data) VALUES (:id, :data)'
);
// Bind data received by form
$query->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
$query->bindParam(':data', $_POST['image'], PDO::PARAM_LOB);
$query->execute();
?>

希望它有所帮助!

编辑:帮助从Android发送数据:

Here is a useful post

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/yourscript.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("image", image.toByteArray()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
} 

An maybe here is another useful one

对不起,我还没有对它进行测试,认为这是一个提示。问题是关于PHP方面。

祝你好运!

答案 2 :(得分:0)

dot=e[grep("\\.", e$e1),]