【发布时间】:2014-01-26 12:31:50
【问题描述】:
您好,我正在尝试使用 PHP 将文件从我的 android 应用程序上传到我的服务器。
我已经阅读了这篇文章:
How to upload a file using Java HttpClient library working with PHP http://www.veereshr.com/Java/Upload How do I send a file in Android from a mobile device to server using http?
这是我的 JAVA 代码:
public void upload() throws Exception {
File file = new File("data/data/com.tigo/databases/exercise");
Log.i("file.getName()", file.getName());
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://***.***.***.***/backDatabase.php");
InputStreamEntity reqEntity = new InputStreamEntity( new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true);
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
if((response.getStatusLine().toString()).equals("HTTP/1.1 200 OK")){
// Successfully Uploaded
Log.i("uploaded", response.getStatusLine().toString());
}
else{
// Did not upload. Add your logic here. Maybe you want to retry.
Log.i(" not uploaded", response.getStatusLine().toString());
}
httpclient.getConnectionManager().shutdown();
}
这是我的 PHP 代码:
<?php
$uploads_dir = '/tigo/databaseBackup';
if (is_uploaded_file($_FILES['exercise']['tmp_name']))
{
$info = "File ". $_FILES['exercise']['name'] ." uploaded successfully.\n";
$file = 'emailTest.log';
file_put_contents($file, $info, FILE_APPEND | LOCK_EX);
move_uploaded_file ($_FILES['exercise'] ['tmp_name'], $_FILES['exercise'] ['name']);
}
else
{
$info = "Possible file upload attack: ";
$file = 'emailTest.log';
file_put_contents($file, $info, FILE_APPEND | LOCK_EX);
$info = "filename '". $_FILES['exercise']['tmp_name'] . "'.";
file_put_contents($file, $info, FILE_APPEND | LOCK_EX);
print_r($_FILES);
}
?>
在我的 logcat 中,我得到 HTTP/1.1 200 OK。 当我查看服务器日志时,我收到此错误:
PHP Notice: Undefined index: exercise in /var/www/backDatabase.php on line 23
我也试过用:
$_FILES['userfile']['name']
代替
$_FILES['exercise']['tmp_name']
我的服务器日志中出现了同样的错误。
我认为我的问题是我无法引用我上传的文件。
感谢您的帮助。
【问题讨论】: