-2

It's morning there. Is it possible that I just forget a "," on my code but I did not see where the problem is.

Here is my html code:

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

Here my php code:

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

When I submit my form I got this : Notice: Undefined variable: imgpress in C:\wamp64\www*******\forms.php on line 65

Cœur
  • 37,241
  • 25
  • 195
  • 267
Cherif K.
  • 1
  • 1
  • The issue is that you define the variable inside the `if` conditional block. If that is _not_ entered because the condition is false, then the variable is not defined when it is used further down. You should either define a default value or add an `else` part to the conditional. – arkascha Jun 30 '17 at 08:26
  • Because the extension of the uploaded file is not in the extension's whitelist thus it's not setting the variable `$imgpress` – DarkBee Jun 30 '17 at 08:27
  • For sending files there's an `enctype` attribute for a form – u_mulder Jun 30 '17 at 08:34
  • thx @u_mulder i'm so silly .. thanks you so much you save my day :D – Cherif K. Jun 30 '17 at 08:35

1 Answers1

0

In case when (in_array($extension, $extension_valid) && in_array($mime, $mime_valid)) == false, $imgpress is undefined

buildok
  • 785
  • 6
  • 7