我正在使用最新的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. rn";
} else {
echo "Temp file not uploaded. rn";
}
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 替换,但在创建文件时无法识别。
任何帮助非常感谢。
要连接,请使用点而不是加号。
$currentName = 'image'.$i;
"图像"+0 只是 0。然后将"image0"替换为$currentName。不要忘记修复以下行,您应该使用"image0",而不是"image"
if ($_FILES['image']['size'])...