php - Inserting files image array into a database

881

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

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'];

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

    }
}
?>

And the Form mockup;

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

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

Can anyone help?

928

Answer

Solution:

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++;
}

Then use it:

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

People are also looking for solutions to the problem: php - Laravel 4.2 Date Validation

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.