仅使用一个控制器将两个图像保存在不同的文件夹中


Saving two images in different folders using only one controller

我构建了一个保存三个参数的界面:标题、价格和图像。标题和价格保存在 SQL 数据库中,图像保存在服务器上的文件夹中。当然,它们都共享一个 ID,因此可以在需要时调用它们。现在我希望能够使用相同的控制器将两个不同的图像(假设空白和示例)保存到两个不同的文件夹(模板/空白和模板/示例)中。这是接口和控制器的代码。可能吗?

templates_edit.php(视图)

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

模板.php(控制器)

<?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');
}
当然,

现在您有两个输入字段,name='img'您需要更改它,其中一个需要不同的名称。

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

然后在控制器中,您需要使用适当的名称存储每个图像。可能需要检查以确保它们也已设置:

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

显然,您会对编辑案例和删除案例执行类似操作。