为什么我得到“未定义的变量”

时间:2017-06-30 08:24:03

标签: php

那天早上好。是否有可能在我的代码中忘记了“,”但我没有看到问题所在。

这是我的HTML代码:

<div class="col-sm-9 col-md-10 affix-content" id="addpress">
                <div class="container">
                    <div class="page-header">
                        <h3><span class="glyphicon glyphicon-th-list"></span> Ajouter une coupure de presse</h3>
                    </div>
                    <form method="POST" class="contact-form mid" action="forms.php">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <input type="text" class="form-control" name="journal" autocomplete="off" id="journal" placeholder="Nom du journal" >
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <input type="text" class="form-control" name="date" autocomplete="off" id="date" placeholder="Date de parution">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <input type="text" class="form-control" name="link" autocomplete="off" id="link" placeholder="Lien">
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <input type="file" class="form-control" name="imgpress"  id="imgpress" >
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <button type="submit" class="btn main-btn pull-right" name="addpressform">Ajouter la coupure de presse</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

这是我的php代码:

if(isset($_POST['addpressform'])){
    $journal = htmlentities($_POST['journal']);
    $date = htmlentities($_POST['date']);
    $link = htmlentities($_POST['link']);

    if (!empty($_FILES)) {
    $mime_valid = ['image/png', 'image/jpeg','image/gif'];
    $extension_valid = ['png', 'jpeg','jpg','gif'];
    $extension = pathinfo($_FILES['imgpress']['name'])['extension'];
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime = finfo_file($finfo, $_FILES['imgpress']['tmp_name']);
    if(in_array($extension, $extension_valid) && in_array($mime, $mime_valid)){
            $imgpress = $_FILES['imgpress']['name'];    
            if(move_uploaded_file($_FILES['imgpress']['tmp_name'], '../views/img/' . $imgpress)){
            echo"Le fichier à bien etais déplacer";
            }else{
                echo"Erreur dossier introuvable";
            }
        } else {
            echo 'Erreur de format';
        }
    }

    $new = array($imgpress, $link, $journal, $date);

    $query->insert("presse", $col=array("img", "link", "journal", "date"), $new);
    //header('Location: home.php#gererpresse');
}

当我提交表单时,我得到了这个:注意:未定义的变量:在第65行的C:\ wamp64 \ www ******* \ forms.php中imgpress

1 个答案:

答案 0 :(得分:0)

如果(in_array($ extension,$ extension_valid)&amp;&amp; in_array($ mime,$ mime_valid))== false,$ imgpress未定义