上传多个图像,PHP [英] Upload multiple images, PHP

查看:83
本文介绍了上传多个图像,PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要修改一张图片上传表格,以便可以上传多个图片(准确地说是6张).我尝试添加其他输入并按照其他人的建议更改名称值,但是它仍然显示与第一个输入相同的图像.我知道这可能很简单,但是我不是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';
        }
    }

对于输入:

<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.

谢谢

推荐答案

您需要具有多个输入字段:

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...

或者您可以使用特定于PHP的数组表示法快捷方式:

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

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

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

请注意,当使用数组表示法时,PHP使文件数组中的每个单独的数据点成为其自己的数组.而不是将文件的数据集中在一个arra中,而是将每个单独的位分布在多个数组中.

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.

这篇关于上传多个图像,PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆