小弟之前没有接触过PHP。如今想实现的功能是,使用android终端,将android终端上的一幅图片传送到php的网站上。
PHP接受数据并存储,之后返回给ANDROID终端一个消息。请问PHP方面如何实现呢?

解决方案 »

  1.   

    简单,就是android用http post的方式把图片传给php,php就是普通的上传文件处理。给你段android代码
            String BOUNDARY = java.util.UUID.randomUUID().toString();
            String PREFIX = "--", LINEND = "\r\n";
            String MULTIPART_FROM_DATA = "multipart/form-data";
            String CHARSET = "UTF-8";
            String data = "";
            
            try {
                URL uri = new URL(url);
                HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
                conn.setReadTimeout(5 * 1000); 
                conn.setDoInput(true);// 允许输入
                conn.setDoOutput(true);// 允许输出
                conn.setUseCaches(false); 
                conn.setRequestMethod("POST");  //Post方式
                conn.setRequestProperty("connection", "keep-alive");
                conn.setRequestProperty("Charsert", "UTF-8");
                conn.setRequestProperty("User-Agent", sUserAgent); // 设置客户端标识
                conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA
                        + ";boundary=" + BOUNDARY);
                
                // 首先组拼文本类型的参数
                StringBuilder sb = new StringBuilder();
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    sb.append(PREFIX);
                    sb.append(BOUNDARY);
                    sb.append(LINEND);
                    sb.append("Content-Disposition: form-data; name=\""
                            + entry.getKey() + "\"" + LINEND);
                    sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);
                    sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
                    sb.append(LINEND);
                    sb.append(entry.getValue());
                    sb.append(LINEND);
                } 
                
                DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
                outStream.write(sb.toString().getBytes());            // 发送文件数据
                if (files != null)
                    for (Map.Entry<String, File> file : files.entrySet()) {
                        StringBuilder sb1 = new StringBuilder();
                        sb1.append(PREFIX);
                        sb1.append(BOUNDARY);
                        sb1.append(LINEND);
                        sb1.append("Content-Disposition: form-data; name=\"fileupload\"; filename=\""
                                        + file.getKey() + "\"" + LINEND);
                        sb1.append("Content-Type: image/jpeg" + LINEND);
                        //sb1.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINEND);
                        sb1.append(LINEND);
                        outStream.write(sb1.toString().getBytes());                    InputStream is = new FileInputStream(file.getValue());
                        byte[] buffer = new byte[1024];
                        int len = 0;
                        while ((len = is.read(buffer)) != -1) {
                            outStream.write(buffer, 0, len);
                        }                    is.close();
                        outStream.write(LINEND.getBytes());
                    }
                
                // 请求结束标志
                byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
                outStream.write(end_data);
                outStream.flush();
                
                // 得到响应码
                int res = conn.getResponseCode(); 
            String line = null;
            InputStream in = conn.getInputStream();
            InputStreamReader isReader = new InputStreamReader(in);
            BufferedReader bufReader = new BufferedReader(isReader);
                while((line = bufReader.readLine())!=null)
                    data += line;
                
    if (res != 200) {
    throw new ApiException("Invalid response from server: "
    + data);
    }

    in.close();
    outStream.close();
    conn.disconnect();
    } catch (IOException e) {
    throw new ApiException("Problem communicating with API", e);
    }
      

  2.   

    一样是tcp/ip通信协议,和PC浏览器一个样吧
    假设POST上传的文件name='hello'
    只需要用到move_uploaded_file()函数和$_FILE超全局变量$tmpName = $_FILES['filename']['tmp_name']; //文件的临时存储位置
    $nowName = ''; // 要移动到的位置
    if(move_uploaded_file($tmpName, $nowName))
        echo 'ok';
    else
        echo 'error';
    我没用过所以不熟,更准确的例子 http://php.net/manual/en/function.move-uploaded-file.php
      

  3.   


    Android端我会,我问的是PHP端的。不过还是谢谢你。
      

  4.   


    我问的是PHP端如何接受二进制数据,并且将其转换成图片的方法。你这个好像是保存文件的方法。
      

  5.   

    $binaryData= $GLOBALS['HTTP_RAW_POST_DATA']; 
    file_put_contents('img.png', $binaryData);应该是这样的。但是如何从PHP返回给android消息说明上传成功了?是通过int res = conn.getResponseCode();  方法,判断res来确定是否上传成功了么?小弟PHP菜鸟,高手来啊。
      

  6.   

    $binaryData= $GLOBALS['HTTP_RAW_POST_DATA'];  
    $file_size = file_put_contents('img.png', $binaryData);
    if($file_size == 文件本身长度 || $file_size >0) echo '成功';
    else echo '失败';
      

  7.   


    如果我已经将图片保存了,那如何上传呢?而不是直接post二进制数据。