【问题标题】:Save in DB 3 images at the same time同时保存在 DB 3 图像中
【发布时间】:2014-02-24 14:46:52
【问题描述】:

我有一个基本的图像上传,它工作得很好,但现在我需要将它保存在我的 MySql DB 3 图像中,而不仅仅是一个,如何使用我的上传脚本以相同的形式保存 3 个图像?

我需要保存 3 张图片,因为我的页面中是一篇文章博客,并且我想在预览的滑块中显示 3 张图片,因此,我需要将 3 张图片保存在同一个 id 中。

另一个想法是,如果上传脚本显示一两个甚至三个文件都是空的,如何只保存一两个而不显示错误?

这里我给你看我的上传脚本:

<?php 
    require_once("connection.php");
    require_once("settings.php");
    $alert = "";
    if(isset($_FILES['foto_ser1'])) {
        $extension = pathinfo($_FILES['foto_ser1']['name']);
        $extension = $extension["extension"];
        $allowed_paths = explode(", ", $allowed_ext);
        $valid = 0;
        for($i = 0; $i < count($allowed_paths); $i++) {
            if ($allowed_paths[$i] == "$extension") {
                $valid = 1;
            }
        }
        if ($valid == 1 && $_FILES["foto_ser1"]["size"] <= $max_weight) {
            if (file_exists("../assets/img/servicios/" . $_FILES["foto_ser1"]["name"])) {
                $alert = '<p class="error">' . $_FILES["foto_ser1"]["name"] . ' El nombre del archivo ya existe!' . '</p>';
            } else {
                move_uploaded_file($_FILES["foto_ser1"]["tmp_name"], "../assets/img/servicios/" . $_FILES["foto_ser1"]["name"]);
                $save1 = $_FILES["foto_ser1"]["name"];
                $statement = $conn->prepare("INSERT INTO SERVICIOS (titulo, descripcion, categoria, foto_ser1, foto_ser2, foto_ser3) VALUES (?, ?, ?, ?, ?, ?)");
                if ($statement->execute(array($_POST['titulo'],$_POST['descripcion'],$_POST['categoria'],$save1,$save2,$save3))); 
                $dbSuccess = true; 
                $alert = '<p class="ok">' . ' Servicio agregado satisfactoriamente!' . '</p>';
                $dbh = null;
            }
        } else {
            $alert = '<p class="error">' . ' Tipo de archivo inv&aacute;lido!' . '</p>';
        }
    }
?>

表单 page.php:

    <form class="form-horizontal" id="servicios" name="data" method="post" enctype="multipart/form-data">
        <fieldset>
            <?php echo $alert1; ?>
            <div class="control-group">
                <label class="control-label col-md-4"><?php echo $translate->__('Title'); ?> :</label>
                <div class="col-md-5">
                    <input type="text" class="form-control" name="titulo" />
                </div>
            </div>
            <div class="control-group">
                <label class="control-label col-md-4"><?php echo $translate->__('Article info'); ?> :</label>
                <div class="col-md-5">
                    <textarea id="maxlength_textarea" class="form-control" maxlength="225" name="descripcion" /></textarea>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label col-md-4"><?php echo $translate->__('Article category'); ?> :</label>
                <div class="col-md-5">
                    <input type="text" class="form-control" name="categoria" />
                </div>
            </div>                  
            <div class="control-group">
                <label class="control-label col-md-4"><?php echo $translate->__('File to upload 1'); ?> :</label>
                <div class="col-md-3">
                    <input name="foto_ser1" type="file" />
                </div>
            </div>                  
            <div class="control-group">
                <label class="control-label col-md-4"><?php echo $translate->__('File to upload 2'); ?> :</label>
                <div class="col-md-3">
                    <input name="foto_ser2" type="file" />
                </div>
            </div>

            <div class="control-group">
                <label class="control-label col-md-4"><?php echo $translate->__('File to upload 3'); ?> :</label>
                <div class="col-md-3">
                    <input name="foto_ser3" type="file" />
                </div>
            </div>

            <div class="control-group">
                <div class="row">
                    <div class="col-md-12">
                        <div class="col-sd-offset-9 col-md-12"><br />
                            <button class="btn btn-info" name="enviar"><i class="fa fa-check"></i> <?php echo $translate->__('Save'); ?></button>
                        </div>
                    </div>
                </div>
            </div>          
        </fieldset>
    </form>
    <div id="loading" style="display:none;"><img src="assets/img/ajax_loader.gif" /></div>

编辑


新代码:

<?php 
    require_once("includes/connection.php");
    require_once("includes/settings.php");
    $alert = "";
    if(isset($_FILES['foto_ser{$i}'])) {
        for($i = 1; $i <= 3; $i++) {
            if ($_FILES["foto_ser{$i}"]['error'] === UPLOAD_ERR_OK) {

                if ($valid == 1 && $_FILES["foto_ser{$i}"]["size"] <= $max_weight) {
                    if (file_exists("assets/img/servicios/" . $_FILES["foto_ser{$i}"]["name"])) {
                        $alert = '<div class="alert alert-block alert-danger fade in">
                    <button type="button" class="close" data-dismiss="alert"></button>
                    <h4 class="alert-heading">Error!</h4>
                    <p>' . $_FILES["foto_ser{$i}"]["name"] . ' El nombre de la foto ya existe!' . '</p></div>';
                    } else {
                        move_uploaded_file($_FILES["foto_ser{$i}"]["tmp_name"], "assets/img/servicios/" . $_FILES["foto_ser{$i}"]["name"]);
                        $save1 = $_FILES["foto_ser{$i}"]["name"];
                        $save2 = $_FILES["foto_ser{$i}"]["name"];
                        $save3 = $_FILES["foto_ser{$i}"]["name"];
                        $activo = is_array($_POST['activo'])
                            ? implode(', ', $_POST['activo'])
                            : $_POST['activo'];
                        $statement = $conn->prepare("INSERT INTO SERVICIOS (titulo_ser, stitulo_ser, servicios, precio, foto_ser1, foto_ser2, foto_ser3, categoria, subcategoria, visto, activo) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
                        if ($statement->execute(array($_POST['titulo_ser'],$_POST['stitulo_ser'],$_POST['servicios'],$_POST['precio'],$save1,$save2,$save3,$_POST['categoria'],$_POST['subcategoria'],$_POST['visto'],$activo))); 
                        $dbSuccess = true; 
                        $alert = '<div class="alert alert-block alert-success fade in">
                        <button type="button" class="close" data-dismiss="alert"></button>
                        <h4 class="alert-heading">Success!</h4>' . ' Nuevo servicio agregado satisfactoriamente!' . '</p></div>';
                        $dbh = null;
                    }
                } else {
                    $alert = '<div class="alert alert-block alert-danger fade in">
                    <button type="button" class="close" data-dismiss="alert"></button>
                    <h4 class="alert-heading">Error!</h4>
                    <p>' . ' Tipo de imagen inv&aacute;lida!' . '</p></div>';
                }
            }
        }
    }
?>

【问题讨论】:

标签: php jquery mysql image file-upload


【解决方案1】:

您需要进行上传处理。现在您的代码只是假设所有上传都会成功。事实上,它假定永远不会没有上传。

简而言之,你需要这样的东西:

for ($i = 1; $i <= 3; $i++) {
   if ($_FILES["foto_ser{$i}"]['error'] === UPLOAD_ERR_OK) {
       ...file #$i has succeeded...
   }
}

您的代码也很危险 - 您在 move_uploaded_files 调用中直接使用用户提供的 ['name']参数,这允许恶意用户在您的服务器上随意书写他们选择的文件。

【讨论】:

  • 嗨,我用你的代码更改了代码并删除了一些想法......在我的提琴手中不要给我错误给我显示所有输入的上传脚本还有 3 个图像但在数据库中没有什么都不省。在编辑行中告诉你我是如何改变代码的......也许你可以看到我的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-19
  • 2020-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多