0

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?

Akshay Vaghasiya
  • 1,597
  • 9
  • 36
  • 60
  • Actualy I asked half question. Main problem area is that how to store image in upload folder? – Akshay Vaghasiya Feb 27 '15 at 12:30
  • You can google for best practices o how to store image. There are many possibilities. – bcesars Feb 27 '15 at 12:32
  • Refer this link to store image in upload folder: http://stackoverflow.com/questions/27164016/upload-images-to-two-different-folders-in-server?noredirect=1#comment42821567_27164016 – Shahzad Barkati Feb 27 '15 at 12:40

3 Answers3

0

use

file_get_contents($file)

That will return string content that you can store as TEXT in mysql DB.

Mihir Bhatt
  • 3,019
  • 2
  • 37
  • 41
0

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!

Edit: help sending data from 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.

Sorry, I haven't test it, consider it as a tip. The question is about PHP side.

Good luck!

Community
  • 1
  • 1
OscarGarcia
  • 1,995
  • 16
  • 17
0
<?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']));
?>
  • 3
    You should avoid code only answers. Instead explain how the code works and how it answers the OP's question – Stewart_R May 01 '16 at 15:20