Question

I have built an interface that saves three parameters: Title, price and an image. The title and price are saved on an SQL database and the image is saved in a folder on the server. Of course they all share one id, so they can be called when needed. Now I want to be able to save two different images (let's say blank and example) into two different folders (template/blank and template/example) using the same controller. Here's the code of both interface and controller. Is it possible?

templates_edit.php (view)

<ol class="breadcrumb">
<li><a href="/admin/home">Templates</a></li>
<li class="active"><?= $title ?></li>
</ol>


<form class="form-horizontal" role="form" method="POST" enctype="multipart/form-data">
<div class="form-group">
    <label for="inputEmail3" class="col-sm-2 control-label">Title</label>
    <div class="col-sm-10">
        <input type="text" name="titlep" value="<?= @$template['titlep'] ?>" class="form-control" autocomplete="off" required="true">
    </div>
</div>
<div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">Price</label>
    <div class="col-sm-10">
        <input type="text" class="form-control" name="price" value="<?= @$template['price'] ?>" autocomplete="off" required="true">
    </div>
</div>
<div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">Blank</label>
    <div class="col-sm-10">
        <input type="file" class="form-control" name="img">
        <?php if (!empty($template)) { ?>
            <img src="/files/templates/blank<?= $template['id'] ?>.jpg" width="100" class="thumbnail">
        <?php } ?>
    </div>
</div>
<div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">Example</label>
    <div class="col-sm-10">
        <input type="file" class="form-control" name="img">
        <?php if (!empty($template)) { ?>
            <img src="/files/templates/example<?= $template['id'] ?>.jpg" width="100" class="thumbnail">
        <?php } ?>
    </div>
</div>
<div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
        <?php
        if (!empty($template)) {
            ?><input type="hidden" name="id" value="<?= $template['id'] ?>"><?php
        }
        ?>
        <input type="submit" name="save" class="btn btn-default" value="Save">
    </div>
</div>

templates.php (controller)

<?php

$tpl = 'templates';
$active = 'templates';

switch ($url[3]) {
case 'add':
    $tpl = 'templates_edit';
    $title = 'Add template';

    if (!empty($_POST['save'])) {
        $DB->prepare('INSERT INTO templates (titlep, price) VALUES (:titlep, :price)')
            ->execute(array(':titlep' => $_POST['titlep'], ':price' => $_POST['price']));
        $id = $DB->lastInsertId();
        $img = new Imagick($_FILES['img']['tmp_name']);
        $img->setImageFormat('jpg');
        $img->writeImages('files/templates/' . $id . '.jpg', true);
        header('Location: /admin/templates');
    }

    break;
case 'edit':
    $title = 'Edit template';
    $tpl = 'templates_edit';

    $template = $DB->query('SELECT * FROM templates WHERE id='.$url[4])->fetch();

    if (!empty($_POST['save'])) {
        $DB->prepare('UPDATE templates SET titlep=:titlep, price=:price WHERE id=:id')
            ->execute(array(':titlep' => $_POST['titlep'], ':price' => $_POST['price'], ':id' => $_POST['id']));
        if (is_file($_FILES['img']['tmp_name'])) {
            $img = new Imagick($_FILES['img']['tmp_name']);
            $img->setImageFormat('jpg');
            $img->writeImages('files/templates/' . $_POST['id'] . '.jpg', true);
        }
        header('Location: /admin/templates');
    }
    break;
case 'delete':

    $DB->query('DELETE FROM templates WHERE id='.$url[4]);
    @unlink('files/templates/'.$url[4]. '.jpg');
    header('Location: /admin/templates');

    break;
default:
    $templates = $DB->query('SELECT * FROM templates');
}
Was it helpful?

Solution

Sure right now you have both input fields with name='img' you need to change this, one of them needs a different name.

<div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">Example</label>
    <div class="col-sm-10">
        <input type="file" class="form-control" name="img_example">
        <?php if (!empty($template)) { ?>
            <img src="/files/templates/example<?= $template['id'] ?>.jpg" width="100" class="thumbnail">
        <?php } ?>
    </div>
</div>

Then in your controller you need to store each of the images with the appropriate name. Might want to check to make sure they are set also:

if(isset($_FILES['img'])) {
    $img = new Imagick($_FILES['img']['tmp_name']);
    $img->setImageFormat('jpg');
    $img->writeImages('files/templates/blank' . $id . '.jpg', true);
}
if(isset($_FILES['img_example'])) {
    $img_example = new Imagick($_FILES['img_example']['tmp_name']);
    $img_example->setImageFormat('jpg');
    $img_example->writeImages('files/templates/example' . $id . '.jpg', true);
}

Obviously you would do something similar for the edit case and the delete case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top