duandeng2011 2015-04-04 03:01
浏览 57
已采纳

将多个图像文件数组插入数据库[重复]

This question is an exact duplicate of:

I am creating a stock form which includes up to 20 image files. When uploading the images to the server i need to change the name and also insert them into a database so that it will work on the websites carousel. The name changes OK aswell as the upload to the servers folder. The problem I seem to be having is that if I CHOOSE ALL the images it works just fine, but if I CHOOSE less, (i.e; 1, 2, 3, 4 etc) then it breaks. I am probably over looking the obvious. Any help - Any ideas??

Here is where I am;

<?php

if(Input::exists()) {

    if(Token::check(Input::get('token'))) {

        try {

            if(!empty($_FILES['image']['name'][0])) {

                $files = $_FILES['image'];

                $i = 0;
                $uploaded = array();

                $failed = array();

                $allowed = array('gif', 'png', 'jpg', 'jpeg');

                foreach($files['name'] as $position => $file_name) {

                    $file_tmp = $files['tmp_name'][$position];
                    $file_size = $files['size'][$position];
                    $file_error = $files['error'][$position];

                    $file_ext = explode('.', $file_name);
                    $file_ext = strtolower(end($file_ext));

                    if(in_array($file_ext, $allowed)) {

                        if($file_error === 0) {

                            if($file_size <= 2097152) {// 2MB

                                $file_name_new = uniqid('', true) . '.' . $file_ext;
                                $file_destination = 'assets/uploads/' . $file_name_new;

                                if(move_uploaded_file($file_tmp, $file_destination)) {
                                    $uploaded[$position] = $file_destination;
                                    $image = ($i === 0) ? 'image' : 'image_' . $i;
                                    $i++;
                                } else {
                                    $failed[$position] = "[{$file_name}] failed to upload";
                                }

                            } else {
                                $failed[$position] = "[{$file_name}] is too large";
                            }

                        } else {
                            $failed[$position] = "[{$file_name}] errored with code [{$file_error}]";
                        }

                    } else {
                        $failed[$position] = "[{$file_name}] file extension '{$file_ext}' is not allowed";
                    }

                }

            }

            $insert = DB::getInstance()->insert('stock', array(
                    'image'         =>  $uploaded[0],
                    'image_1'       =>  $uploaded[1],
                    'image_2'       =>  $uploaded[2],
                    'image_3'       =>  $uploaded[3],
                    'image_4'       =>  $uploaded[4],
                    'image_5'       =>  $uploaded[5],
                    'image_6'       =>  $uploaded[6],
                    'image_7'       =>  $uploaded[7],
                    'image_8'       =>  $uploaded[8],
                    'image_9'       =>  $uploaded[9],
                    'image_10'      =>  $uploaded[10],
                    'image_11'      =>  $uploaded[11],
                    'image_12'      =>  $uploaded[12],
                    'image_13'      =>  $uploaded[13],
                    'image_14'      =>  $uploaded[14],
                    'image_15'      =>  $uploaded[15],
                    'image_16'      =>  $uploaded[16],
                    'image_17'      =>  $uploaded[17],
                    'image_18'      =>  $uploaded[18],
                    'image_19'      =>  $uploaded[19]
            ));

        } catch(Exception $e) {
            die($e->getMessage());
        }

    }
}
?>

And the Form mockup;

<form action="" method="post" enctype="multipart/form-data">

<div class="row">
    <div class="form-group">
        <div class="col-md-6">
            <label class="block"><strong>Upload Vehicle Images</strong><br>(Min = 1)<br>(Max = 20)</label><br><br>
            <input type="file" name="image[]" id="file" class="form-control" tabindex="29"><br><br>
            <input type="file" name="image[]" id="file" class="form-control" tabindex="30"><br><br>
            <input type="file" name="image[]" id="file" class="form-control" tabindex="31"><br><br>
            <input type="file" name="image[]" id="file" class="form-control" tabindex="32"><br><br>
            <input type="file" name="image[]" id="file" class="form-control" tabindex="33"><br><br>
            <input type="file" name="image[]" id="file" class="form-control" tabindex="34"><br><br>
            <input type="file" name="image[]" id="file" class="form-control" tabindex="35"><br><br>
            <input type="file" name="image[]" id="file" class="form-control" tabindex="36"><br><br>
            <input type="file" name="image[]" id="file" class="form-control" tabindex="37"><br><br>
            <input type="file" name="image[]" id="file" class="form-control" tabindex="38"><br><br>
            <input type="file" name="image[]" id="file" class="form-control" tabindex="39"><br><br>
            <input type="file" name="image[]" id="file" class="form-control" tabindex="40"><br><br>
            <input type="file" name="image[]" id="file" class="form-control" tabindex="41"><br><br>
            <input type="file" name="image[]" id="file" class="form-control" tabindex="42"><br><br>
            <input type="file" name="image[]" id="file" class="form-control" tabindex="43"><br><br>
            <input type="file" name="image[]" id="file" class="form-control" tabindex="44"><br><br>
            <input type="file" name="image[]" id="file" class="form-control" tabindex="45"><br><br>
            <input type="file" name="image[]" id="file" class="form-control" tabindex="46"><br><br>
            <input type="file" name="image[]" id="file" class="form-control" tabindex="47"><br><br>
            <input type="file" name="image[]" id="file" class="form-control" tabindex="48"><br>
        </div>
    </div>
</div>
<br>
<button class="btn btn-primary disabled" type="submit">FORM SUBMIT</button>
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">

Can anyone help?

</div>
  • 写回答

1条回答 默认 最新

  • dongshang5862 2015-04-04 03:12
    关注

    Please add a check within your for loop:

    foreach($files['name'] as $position => $file_name) {
        $uploaded[$position] = '';
        if(!empty($file_name)) {
            //your code here
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 根据以下文字信息,做EA模型图
  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥60 关机时蓝屏并显示KMODE_EXCEPTION_NOT_HANDLED,怎么修?
  • ¥66 如何制作支付宝扫码跳转到发红包界面