[英]Android and upload file to php server
我正在編寫一個應將文件發送到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方面輕松解決問題嗎?
我認為問題出在這里:
InputStreamBody isb= new InputStreamBody(new ByteArrayInputStream(data), "file");
這將發送名稱為“文件”的數據,但是
$_FILES['uploadedfile']['name']
正在嘗試查找名為“ uploadedfile”的文件。 確保它們匹配。
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.