将文件图像数组插入数据库 [英] Inserting files image array into a database

查看:68
本文介绍了将文件图像数组插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个库存表单,其中包含多达20个图像文件.当将图像上传到服务器时,我需要更改名称并将其插入数据库中,以便它可以在网站轮播上使用.名称和上传都将更改为OK.我似乎遇到的问题是,如果我选择所有图像,它就可以正常工作,但是如果我选择的图像少,它就会损坏.我可能正在寻找显而易见的东西.任何帮助-任何想法?

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. The problem I seem to be having is that if I CHOOSE ALL the images it works just fine, but if I only CHOOSE less then it breaks. I am probably over looking the obvious. Any help - Any ideas??

我在这里;

<?php

if(Input::exists()) {

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

        try {

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

                $files = $_FILES['image'];

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

任何人都可以帮忙吗?

推荐答案

不要对上传数组的索引进行编码. 而是动态地构建数组:

Don't harcode the indices of the upload array. Instead build you array dynamically:

$i = 0;
$uploads = array();
if(move_uploaded_file($file_tmp, $file_destination)) {
    $uploaded[$position] = $file_destination;
    $image = ($i==0) ? 'image' : 'image_'.$i;
    $uploads[$image]=$uploaded;
    $i++;
}

然后使用它:

 $insert = DB::getInstance()->insert('stock', $uploads);

这篇关于将文件图像数组插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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