【发布时间】:2015-02-23 11:47:05
【问题描述】:
我正在尝试将图像从我的 android 应用程序上传到运行 php/mysql 的服务器。这是安卓代码
private static String UploadImageHelper(Context context, Bitmap bitmap, String url) throws ClientProtocolException, IOException {
if (bitmap != null) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, bos);
byte[] data = bos.toByteArray();
String fileName = "image";
ByteArrayBody bab = new ByteArrayBody(data, fileName);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("uploadedfile", bab);
Pair<String, Integer> pair = httpRequestHelper(url, reqEntity);
return pair.first;
}
return null;
}
private static Pair<String, Integer> httpRequestHelper(String url, HttpEntity reqEntity) {
try {
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = TIMEOUT_SECONDS * 1000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = TIMEOUT_SECONDS * 1000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpPost httppost = new HttpPost(url);
httppost.setEntity(reqEntity);
HttpClient client = new DefaultHttpClient(httpParameters);
HttpResponse entity = client.execute(httppost);
int responseCode = entity.getStatusLine().getStatusCode();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
entity.getEntity().writeTo(baos);
byte[] content = baos.toByteArray();
String responseVal = Common.unzipString(content);
//Log.d("IMAGE SAVE", responseVal);
return new Pair<String, Integer>(responseVal, responseCode);
}
catch (Exception e) {
e.printStackTrace();
}
return getErrorReturn();
}
PHP
<?php
if (isset($_FILES['uploadedfile']) &&
isset($_GET['user_id']) &&
isset($_GET['fish_id']) &&
isset($_GET['is_node']) &&
isset($_GET['comment']) &&
isset($_GET['lat']) &&
isset($_GET['long']) &&
isset($_GET['addr'])) {
$user_id = $_GET['user_id'];
$fish_id = $_GET['fish_id'];
$is_node = $_GET['is_node'];
$comment = mysql_real_escape_string($_GET['comment']);
$lat = $_GET['lat'];
$long = $_GET['long'];
$addr = mysql_real_escape_string($_GET['addr']);
if ($is_node=="1") {
$table = "category";
} else {
$table = "species";
}
// add the fish image to the table
$query = "INSERT INTO {$table}_images ({$table}_id,comment,approved,main,date_added,user_id) VALUES ({$fish_id},'{$comment}',1,0,NOW(),{$user_id})";
mysql_query($query);
$pic_id = mysql_insert_id();
$filename = $pic_id . '.png';
$target_path = "./../../pics/{$table}/{$fish_id}/";
// make the directory is not exist
if (!is_dir($target_path)) {
$old_umask = umask(0);
mkdir($target_path, 0777);
umask($old_umask);
}
// move the image there
if(!move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path . $filename)) {
$errorMessage = $GLOBALS['UPLOAD_IMAGE_ERROR'];
logQuery($target_path . $filename);
logQuery($_FILES['uploadedfile']['tmp_name']);
logQuery($errorMessage);
} else {
$location_id = 0;
if ($is_node != "1" && $addr != "") {
// create the new location
$query = "INSERT INTO location (species_id, address, latitude, longitude, comment, approved) VALUES ({$fish_id}, '{$addr}', {$lat}, {$long}, '', 1)";
mysql_query($query);
if (mysql_error() == "") {
$location_id = mysql_insert_id();
}
}
$successMessage = array(
'pic_id' => $pic_id,
'elink' => getExternalLink($table, $fish_id, $pic_id),
'locationId' => $location_id
);
}
} else {
$errorMessage = $GLOBALS['MISSING_INFO'];
}
?>
前两个日志函数产生
./../../pics/species/22/39.png
/tmp/php9YSsDz
第三个日志不相关。这只是我自己的自定义错误消息。
有人知道为什么move_uploaded_file 函数会失败吗?
谢谢
【问题讨论】:
-
尝试使用
$_FILES['uploadedfile']['error']而不是$GLOBALS['UPLOAD_IMAGE_ERROR']获取错误 -
它为此打印了
0。 -
http://php.net/manual/en/features.file-upload.errors.phpError Messages Explained -
哦,我认为是目标文件夹权限。
标签: php android mysql image upload