duanhun3273 2012-03-19 14:51
浏览 22
已采纳

上传多个图片,PHP

I need to amend an image upload form so I can upload more than one image (6 to be exact). I've tried adding another input and changing the name value as suggested by others, but it still shows up with the same image duplicated from the first input. I know it's probably something simple, but I'm not a PHP developer so I haven't been able to find a solution.

Here's the code for uploading images:

if(isset($_FILES['image']) && $_FILES['image']['tmp_name'] != '') {
        $tempext = explode('.', $_FILES['image']['name']);
        $tempext = strtolower($tempext[count($tempext) - 1]);
        switch($tempext) {
            case 'jpg':
            case 'jpeg':
                $ext = '.jpg';
                break;
            default:
                $ext = false;
        }

        if($ext != false && move_uploaded_file($_FILES['image']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/img/profiles/' . $id . $ext)) {
            $imagesuccess = chmod($_SERVER['DOCUMENT_ROOT'] . '/img/profiles/' . $id . $ext, 0644) ? 'yes' : 'no';
        } else {
            $imagesuccess = 'no';
        }
    }

And for the input:

<div class="control-container<?php if((isset($_FILES['image']) && !empty($_FILES['image']['name']))) echo ' error'; ?>">
            <label for="image">Large Image</label>
            <div class="multifield">
                <?php 
                if(file_exists($_SERVER['DOCUMENT_ROOT'] . '/img/profiles/' . $id . '.jpg')) {
                    echo '<img src="/img/profiles/' . $id . '.jpg?' . rand() . '" width="310px" />';
                }
                ?>
                <input type="file" name="image" id="image" />
            </div>
</div>

Any help would be much appreciated.

Thanks

  • 写回答

5条回答 默认 最新

  • dongyi2993 2012-03-19 14:58
    关注

    You'd need to have multiple input fieldS:

    <input type="file" name="image1" />
    <input type="file" name="image2" />
    etc...
    

    and access each individual file with:

    $_FILES['image1'][...]
    $_FILES['image2'][...]
    etc...
    

    or you can use the PHP-specific array notation shortcut:

    <input type="file" name="image[]" />
    <input type="file" name="image[]" />
    

    and

    $_FILES['image']['tmp_name'][0];
    $_FILES['image']['tmp_name'][1];
    etc...
    

    Note that when using the array notation, PHP makes each individual data-point in the files array its own array. Instead of lumping a file's data together in a single arra, each individual bit is spread about across multiple arrays.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!