2012-07-12 45 views
0

我正在寫一個應用程序應該發送文件到PHP服務器。這裏是我的代碼:Android和上傳文件到php服務器

InputStream is = new FileInputStream(file); 
    HttpClient httpClient = new DefaultHttpClient(); 
    HttpPost postRequest = new HttpPost("http://majkelsoftgames.cba.pl/ser/server.php"); 

    byte[] data = IOUtils.toByteArray(is); 
    InputStreamBody isb= new InputStreamBody(new ByteArrayInputStream(data), "file"); 

    MultipartEntity multipartContent = new MultipartEntity(); 
    multipartContent.addPart("file", isb); 

    postRequest.setEntity(multipartContent); 
    HttpResponse response = httpClient.execute(postRequest); 

我的問題是,我真的沒有在PHP的經驗,我不知道如何 拿起PHP端這個文件。我發現一些代碼:

<?php 
    $target_path = "./"; 
    $target_path = $target_path . basename($_FILES['uploadedfile']['name']); 

    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
     echo "The file ". basename($_FILES['uploadedfile']['name'])." has been uploaded"; 
    } 
    else { 
     echo "There was an error uploading the file, please try again!"; 
    } 
?> 

但它不起作用。有人可以解釋我怎麼才能簡單地在PHP方面拿起罰款?

回答

2

我認爲這個問題是在這裏:

InputStreamBody isb= new InputStreamBody(new ByteArrayInputStream(data), "file"); 

這是發送一個名爲「文件」中的數據,但隨後

$_FILES['uploadedfile']['name'] 

正試圖找到一個名爲「UploadedFile的」文件。確保它們匹配。

+0

我改變它,但它不幫助。 – AYMADA 2012-07-13 06:56:46

+1

好的,那麼我需要一些更多的細節。當你說「這行不通」時,你的意思是什麼?你是否收到迴應聲明中的迴應或根本沒有迴應?這樣你就可以知道你是否正確地點擊了頁面。 – 2012-07-13 13:40:57

+1

此外,我發現了一些SO線程與相同的PHP代碼片段,所以我不知道你在哪裏找到它,但[this](http://stackoverflow.com/questions/4295417/upload-a-picture-from -android-to-php-server)線程建議查看你的服務器的設置和[this](http://stackoverflow.com/questions/1314249/upload-and-post-file-to-php-page)有一些伴隨着你可以嘗試的Java代碼。 – 2012-07-13 13:44:31

1
iF YOU WANT TO UPLOAD .pdf FILE TO LOCAL SERVER THEN USE THIS SIMPLE METHOD, Lets we are doing code here under Button Click Event... 

if (isset($_POST['submit'])) 
{ 

if (($_FILES["file"]["type"] =="application/pdf")) 
{ 

if (file_exists("C:/xampplite/htdocs/site/upload/" . $_FILES["file"]["name"])) 

    echo " This File is already exists in folder"; 

else 
{ 
    move_uploaded_file ($_FILES["file"]["tmp_name"],"C:/xampplite/htdocs/site/upload/" . $_FILES["file"]["name"]);  
    echo "File have been Stored in:-C:/xampplite/htdocs/site/upload/ " . $_FILES["file"]["name"]; 

    } 
} 

}//end of click_event 
相關問題