简体   繁体   English

Android并将文件上传到php服务器

[英]Android and upload file to php server

I'm writing an app which should send a file to PHP server. 我正在编写一个应将文件发送到PHP服务器的应用程序。 Here is my code: 这是我的代码:

    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);

My problem is that I really dont have experience in PHP and I dont know how to pick up this file on PHP side. 我的问题是我真的没有PHP的经验,我也不知道如何在PHP方面选择此文件。 I found some code: 我找到了一些代码:

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

But it does not work. 但这行不通。 Can someone explain me how can I simply pick up fine on PHP side? 有人可以向我解释一下如何在PHP方面轻松解决问题吗?

I think the issue is here: 我认为问题出在这里:

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

This is sending the data with the name "file", but then 这将发送名称为“文件”的数据,但是

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

is trying to find a file named "uploadedfile". 正在尝试查找名为“ uploadedfile”的文件。 Make sure they match. 确保它们匹配。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM