我使用最新的 apache HTTPmultipart 将多个图像从 android 发布到 PHP 脚本。
我在android中创建了一个循环来加载这样的多部分:
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(urlServer);
MultipartEntity mpEntity = new MultipartEntity();
//Loop to add images
for (int i = 0; i<selectedList.size(); i++) {
File passFile = selectedList.get(i);
ContentBody cbFile = new FileBody(passFile, "image/png");
String partName = "image" + i;
mpEntity.addPart(partName, cbFile);
}
这工作正常,并使用以下代码将图像加载到我的 PHP 中以获取第一张图像:
uploadDir = './'; //Uploading to directory specified
$file = basename($_FILES['image0']['name']);
$uploadFile = $file;
$randomNumber = rand(0, 99999999999);
$newName = $uploadDir . $randomNumber . $uploadFile;
if (is_uploaded_file($_FILES['image0']['tmp_name'])) {
echo "Temp file uploaded. \r\n";
} else {
echo "Temp file not uploaded. \r\n";
}
if ($_FILES['image']['size']> 100000000) {
exit("Your file is too large.");
}
if (move_uploaded_file($_FILES['image0']['tmp_name'], $newName)) {
$maxsize = ini_get('upload_max_filesize');
echo json_encode(
array(
'result'=>$newName,
'msg'=>'Report added successfully.'
)
);
我遇到了问题,但是将更多图像放入 PHP。我尝试了以下方法:
$currentName = 'image' + 0;
$file = basename($_FILES[$currentName]['name']);
但这不起作用。将创建一个循环,因此上面的 0 将被 i 替换,但这在创建文件时无法识别。
非常感谢任何帮助。