2013-02-06 51 views
0

我想从我的android应用程序张贴图像到PHP文件,我想知道什么格式(文件,Fileoutputstream等)我必须张贴它被认为是一个文件并在我的php脚本中用$ _FILE ['filename']引用它。张贴到PHP文件的图像

谢谢:)

编辑:

对不起,我可能没有清楚,我不是在寻找的PHP脚本,我已经拥有了完成接受$ _FILE [“样本” ]并做我需要它,我只是不知道我必须张贴到PHP文件(IN JAVA)的文件类型,以便PHP''看到它作为$ _FILE

FYI:I我正在使用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()); 
      } 
     }); 
    } 
+0

您可以展示您迄今为止所使用的Android代码,特别是执行HTTP POST到PHP服务器的代码吗? – mbeckish

+0

我已添加代码。 – Jonny07

+0

[从Android(使用Android异步Http客户端)上载图像到rails服务器(使用回形针)](http://stackoverflow.com/questions/12245469/uploading-an-image-from-android-with- android-asynchronous-http-client-to-rails) – mbeckish

回答

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}"; 
} 
?> 
+0

对不起,我不清楚,我更新了我的问题。 – Jonny07

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“是...

+1

对不起,我不能使用这种方法,因为我在主线程上做这个请求,这就是为什么我需要使用异步库。不过谢谢。 哦PS - $ _FILES ['image'] ['tmp_name'];是php临时保存发布的图像,然后你总是将它移动到你想要存储的位置,在这种情况下为$ target。 :P – Jonny07