0

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

谢谢 :)

编辑:

抱歉,我可能不清楚,我不是在寻找 PHP 脚本,我已经完成了接受 $_FILE['sample'] 并用它做我需要的事情,我只是不确定文件类型我必须发布到 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());
            }
        });
    }
4

2 回答 2

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}";
}
?>
于 2013-02-06T14:38:19.633 回答
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”是什么......

于 2013-02-06T14:50:09.860 回答