将图像发布到PHP文件

时间:2013-02-06 14:33:55

标签: java android

我正在尝试将图像从我的Android应用程序发布到PHP文件中,并且想知道我必须发布什么格式(文件,Fileoutputstream等)才能将其识别为文件并用$引用它_PHILE ['filename']在我的php脚本中。

谢谢:)

修改

抱歉,我可能不太清楚,我不是在寻找PHP脚本,我已经完成了接受$ _FILE ['sample']并完成了我需要的东西,我只是不确定文件TYPE,我必须发布到PHP文件(IN JAVA),以便PHP'看到'它为$ _FILE

仅供参考:我正在使用loopj异步http请求库。

public void add_image_android(final Bitmap image, String party_id, String guest_id) 
    { 

        String url = "http://www.mysite.com/urltopost";

            /* not sure what to set fOut to for the bitmap to be passed as file */

        RequestParams params = new RequestParams();
        params.put("file", fOut);
        params.put("guest_id", guest_id);
        params.put("party_id", party_id);
        client.post(url, params, new JsonHttpResponseHandler() 
        {
            @Override
            public void onSuccess(JSONObject response)
            {
                ((ResponseListener)_mainContext).add_image_android_response(response.toString());
                return; 
            }
            @Override
            public void onFailure(Throwable e) 
            {
                fireToast("api error:"+e);
                Log.d("api error:",e.toString());
            }
        });
    }

2 个答案:

答案 0 :(得分:0)

尝试下面的代码,它将上传图像并提供链接。

<?php
$uploaddir = 'images/';
$ran = rand () ;

$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir .$ran.$file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "http://www.domain.com/folder/{$uploadfile}";
}
?>

答案 1 :(得分:0)

这对我有用:(非常古老的代码,希望它有帮助......)

ReturnObject returnObject = new ReturnObject(); 

HttpURLConnection conn = null;
DataOutputStream dos = null;
BufferedReader inStream = null;

String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary =  "*****";

int bytesRead, bytesAvailable, bufferSize;

byte[] buffer;

int maxBufferSize = 1*1024*1024;

String urlString = "your url";

try{
    FileInputStream fileInputStream = new FileInputStream(photoFile);

    URL url = new URL(urlString);
    conn = (HttpURLConnection) url.openConnection();
    conn.setDoInput(true);

    conn.setDoOutput(true);
    conn.setUseCaches(false);

    conn.setRequestMethod("POST");
    conn.setRequestProperty("Connection", "Keep-Alive");
    conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

    dos = new DataOutputStream( conn.getOutputStream() );
    dos.writeBytes(twoHyphens + boundary + lineEnd);
    dos.writeBytes("Content-Disposition: form-data; name=\"image\";"
      + " filename=\"" + photoFile.getAbsolutePath() +"\"" + lineEnd);
    dos.writeBytes(lineEnd);

    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    buffer = new byte[bufferSize];

    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    while (bytesRead > 0){
        dos.write(buffer, 0, bufferSize);
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    }

    dos.writeBytes(lineEnd);
    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

    fileInputStream.close();
    dos.flush();
    dos.close();

}catch (MalformedURLException ex){
    ex.printStackTrace();
}catch (IOException ioe){
    ioe.printStackTrace();
}

在服务器上我发现了这个:

$source = $_FILES['image']['tmp_name'];
move_uploaded_file($source, $target)

不确定这个“tmp_name”是什么......