【问题标题】:Upload multiple images to php script from android从android上传多个图像到php脚本
【发布时间】:2013-01-23 20:10:13
【问题描述】:

我正在使用最新的 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 替换,但在创建文件时无法识别。

非常感谢任何帮助。

【问题讨论】:

    标签: php image file upload http-post


    【解决方案1】:

    要连接使用点而不是加号。

    $currentName = 'image'.$i;
    

    'image'+0 只是 0。然后将 'image0' 替换为 $currentName。不要忘记修复以下行,你应该使用'image0',而不是'image'

    if ($_FILES['image']['size'])...
    

    【讨论】:

    • 菜鸟错了,我已经用 java 编码好几天了!稍后再试,谢谢
    • @EHarpham 如果有帮助,请检查答案
    猜你喜欢
    • 2016-03-13
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 2015-07-13
    相关资源
    最近更新 更多