同时保存在DB 3图像中

时间:2014-02-24 14:46:52

标签: php jquery mysql image file-upload

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

我需要保存3张图片,因为我的页面中有一篇文章博客,我想在预览中的滑块中显示3张图片,因此,我需要在相同的ID中保存3张图片。

另一个想法是,如何在没有上传脚本的情况下保存一两个显示错误,因为上传的一个或两个甚至三个文件都是空的?

我在这里向您展示我的上传脚本:

<?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>';
                }
            }
        }
    }
?>

1 个答案:

答案 0 :(得分:0)

您需要进行上传处理。现在你的代码就是假设所有上传都会成功。实际上,它假设绝不会上传。

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

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

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