Php Android HTTP Post文件

Php Android HTTP Post文件,php,android,http-post,Php,Android,Http Post,目前,我有一个Android应用程序,它通过http将数据发布到一个Web服务器,该服务器有一个php脚本来接收数据并保存到一个文本文件中 以下是代码片段: 安卓: public void postData(String id, String data) { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient();

目前,我有一个Android应用程序,它通过http将数据发布到一个Web服务器,该服务器有一个php脚本来接收数据并保存到一个文本文件中

以下是代码片段:

安卓:

 public void postData(String id, String data) {

         // Create a new HttpClient and Post Header
         HttpClient httpclient = new DefaultHttpClient();
         HttpPost httppost = new HttpPost("<server_ip>/phppost.php");

         try {
             // Add your data
             List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
             nameValuePairs.add(new BasicNameValuePair("id", id));
             nameValuePairs.add(new BasicNameValuePair("stringdata", data));
             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
         }
}
public void postData(字符串id,字符串数据){
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost-HttpPost=newhttppost(“/phppost.php”);
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“id”,id));
添加(新的BasicNameValuePair(“stringdata”,data));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
}
php脚本:

<?php

$id = $_POST['id'];
$file = $id . ".txt";
$data = $_POST['stringdata'];
file_put_contents($file, $data);

?>

但这是针对数据的,它可以毫无问题地工作。如何对文件执行相同的操作?表示*.mp3或*.txt文件。如何发送http post文件以及如何修改php脚本以接收文件

提前谢谢

看看这个

然后像这样调用该函数

 new Thread(new Runnable() {
                        public void run() {
                             runOnUiThread(new Runnable() {
                                    public void run() {
                                      //Show information to user that file is uploading
                                    }
                                });                      

                             uploadFile(uploadFilePath + "" + uploadFileName);

                        }
                      }).start();        
                }
别忘了添加权限

 <uses-permission android:name="android.permission.INTERNET"/>


请检查此链接,不鼓励回答。虽然链接可能会回答这个问题,但最好将链接的内容添加到您的帖子中。答案是否包含内容并不重要。唯一的问题是答案应该指向正确的方向。获得足够的声誉,然后将链接发布为评论和
 <uses-permission android:name="android.permission.INTERNET"/>
if($_FILES){
$file = $_FILES['file'];
$fileContents = file_get_contents($file["tmp_name"]);
//check whether  target dir exist then put contents
if (!is_dir("path"));
file_put_contents("../../uploads/3/dbbackups/".$_FILES['file']['name'], $fileContents);
}