将多个图像文件数组插入数据库

时间:2015-04-04 03:01:47

标签: php mysql

我正在创建一个包含最多20个图像文件的库存表单。将图像上传到服务器时,我需要更改名称并将其插入数据库,以便它可以在网站轮播上使用。名称更改正常以及上载到servers文件夹。我似乎遇到的问题是,如果我选择所有的图像它工作得很好,但如果我选择较少,(即1,2,3,4等)然后它会中断。我可能看起来很明显了。任何帮助 - 任何想法??

这就是我的所在;

<?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());
        }

    }
}
?>

表单模型;

<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(); ?>">

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

请在for循环中添加一张支票:

foreach($files['name'] as $position => $file_name) {
    $uploaded[$position] = '';
    if(!empty($file_name)) {
        //your code here
    }
}