1

I am sending a mp3 file using the following code to a server.In server I want a php code to receive this byte array of mp3 and convert it into file and store it there.

try {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    File file = new File(Environment.getExternalStorageDirectory()+ "/my.mp3");
    final InputStream in = new FileInputStream(file);
    final byte[] buf = new byte[2048];
    int n;
    while ((n = in.read(buf)) >= 0) {
        out.write(buf, 0, n);
    }
    final byte[] data = out.toByteArray();
    String urlString = "http://10.0.0.56/upload.php";
    HttpPost postRequest = new HttpPost(urlString);
    postRequest.setEntity(new ByteArrayEntity(data));
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(postRequest);
    HttpEntity entity = response.getEntity();
    InputStream ins = entity.getContent();
    BufferedReader br = new BufferedReader(new InputStreamReader(ins));
    String temp_str;
    StringBuilder sb = new StringBuilder();
    while((temp_str = br.readLine()) != null) {
        sb.append(temp_str);
    }
    Log.e("response", sb.toString());
} catch (Exception e) {
    // handle exception here
    Log.e(e.getClass().getName(), e.getMessage());
    return "exception";
}

Any one know how to read the mp3 file using php.

2
  • You should be able to find data in $_POST variable. If you were able to write this code in Java, opening binary file in php, writing and then saving it shouldnt be problem for you. Commented Mar 15, 2013 at 10:32
  • @Buksy i m not comfortable with php.i am a java and android guy.i searched a lot but didn't find any post which is directly reading audio file.I got post to read image but not audio file.y. Commented Mar 15, 2013 at 10:33

1 Answer 1

5

Your upload.php could look something like this:

$mp3_bin = file_get_contents('php://input');
file_put_contents( '/path/to/my/mp3', $mp3_bin );

Short and sweet ;)

Edit:

# A bit of golfing gives a one liner:
file_put_contents('/path/to/my.mp3', file_get_contents('php://input'));
Sign up to request clarification or add additional context in comments.

6 Comments

So.. basically the file_get_contents() is reading the raw HTTP POST data and the file_put_contents() is saving the raw content to a file.
<?php $mp3_bin = file_get_contents('php://input'); file_put_contents( '/home/m/m/my.mp3', $mp3_bin ); %>
Because you gave file_put_contents what looks like a directory.. give it a file instead ;)
see my edited comment.Now i made the file but still not working :(.plz do something.boss is dancing on my head.
The only reason this wouldn't work is file permissions.. Does your httpd user have write persmission to the directory and file where you're trying to write ? Otherwise (this usually is the case) attempt writing to /tmp/my.mp3 as /tmp is usually open for any user.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.