I want to store an image(which is passed by an android developer) into mysql database. I'm working in PHP. Plz help me someone to script php code.
Actualy I asked half question. Main problem area is that how to store image in upload folder?
I want to store an image(which is passed by an android developer) into mysql database. I'm working in PHP. Plz help me someone to script php code.
Actualy I asked half question. Main problem area is that how to store image in upload folder?
use
file_get_contents($file)
That will return string content that you can store as TEXT in mysql DB.
You can try this if the image has been sent using POST form 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();
?>
Hope it helps!
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.
Sorry, I haven't test it, consider it as a tip. The question is about PHP side.
Good luck!
<?php
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$query = $conn->prepare('INSERT INTO images_table (id, image_data) VALUES (?, ?)');
$query->execute(array($_POST['id'], $_POST['image']));
?>