php mvc更新图片

时间:2017-07-29 14:45:26

标签: php image model-view-controller edit

我有一些关于在自定义PHP MVC代码上编辑多个图像的问题。

这是控制器

public function izmijeniProizvod() {
    $item['item_id'] = $_POST['item_id'];
    $item['title'] = $_POST['title'];
    $item['description'] = $_POST['description'];
    $item['price'] = $_POST['price'];
    $item['fk_category_id'] = $_POST['fk_category_id'];
    $item['fk_sub_category_id'] = $_POST['fk_sub_category_id'];
    $item['active'] = $_POST['active'];
    $item['sifra_proizvoda'] = $_POST['sifra_proizvoda'];
    $item['image'] = !empty($_FILES['image']['name'][0]) ? $_FILES['image']['name'][0] : '';
    $imageFile = $_FILES['image']['name'];
    $imageFolder = BASE_PATH.'cdn/proizvodi/' . $item['item_id'] . '/';

    $result = $this-> model-> updateItem($item); 



    for($i = 0; $i < count($imageFile); $i++) :
        $item['image'] = !empty($_FILES['image']['name'][$i]) ? $_FILES['image']['name'][$i] : '';


        if (!empty($_FILES['image']['tmp_name'][$i]) && $_FILES['image']['error'][$i] == 0 && $_FILES['image']['size'][$i] > 0) :
            require_once APP . 'PHPThumb/ThumbLib.inc.php';

            if (!is_dir($imageFolder)) :
                mkdir($imageFolder);
            endif;

            if (is_dir($imageFolder)) {
                array_map('unlink', glob($imageFolder . '*'));
            }


                foreach ($this->thumbs as $thumb) :
                    $newImageFile = $thumb['width'] . 'x' . $thumb['height'] . '_' . $imageFile[$i];
                    $phpThumb = PhpThumbFactory::create($_FILES['image']['tmp_name'][$i]);
                    $phpThumb->resize($thumb['width'], $thumb['height'])->save($imageFolder . $newImageFile); //cuva sliku
                endforeach;
                    $this-> model-> updateSlike($item, $imageFile[i]);


        endif;
    endfor;

这部分代码的模型是:

public function updateItem($item) {
    $imageSql = !empty($item['image']) ? " , `image` = '{$item['image']}' " : '';

    $sql = "UPDATE `items` SET `title` = '{$item['title']}', `active` = '{$item['active']}' , `sifra_proizvoda` = '{$item['sifra_proizvoda']}' , `description` = '{$item['description']}', `price` = '{$item['price']}',
                   `fk_category_id` = '{$item['fk_category_id']}', `fk_sub_category_id` = '{$item['fk_sub_category_id']}'
                   $imageSql
            WHERE `item_id` = '{$item['item_id']}'";
    $result = $this->db->exec($sql);
    return $result;

}

public function updateSlike($item){
    //$slika = count($item['image']);

    $sql = "UPDATE `slike_proizvoda` SET `slika1` = '{$item['image']}' WHERE `item_id` = '{$item['item_id']}'";
    $result = $this->db->exec($sql);
    return $result;
}

查看部分代码:

<div class="form-group">
  <label class="col-lg-3 control-label">Slika:</label>
  <div class="col-lg-8">
    <?php if (!empty($this->item['images']['300x300'])) { ?>
      <img id="slika" alt="<?php echo $this->item['image']; ?>"  src="<?php echo $this->item['images']['300x300'] ?>" />
    <?php } ?>
    <div class="help-block">
      <p>
        <label for="image">Izmijeni glavnu sliku:</label>
        <input type="file" name="image[]" id="image">
        <input type="hidden" name="image[]" />
      </p>
      <p>
        <label for="image2">Ubaci još jednu sliku:</label>
        <input type="file" name="image[]" id="image">
        <input type="hidden" name="image[]" />
      </p>
      <p>
        <label for="image3">Ubaci još jednu sliku:</label>
        <input type="file" name="image[]" id="image">
        <input type="hidden" name="image[]" />
      </p>
    </div>
  </div>

我从这部分代码中得到的东西是: 只有一个imaget上传,在updateSlike()上只获得mysql中的一个图像名...

感谢。

0 个答案:

没有答案