2

I have an error when the form is submitted. I try to get an image upload in the input image1 I have specified multipart/form-data , and specified the name value of the input : image1 I also tried with post method and get method but $_FILES still empty the error is : Notice: Undefined index: image1

Here is the HTML code

<form  enctype="multipart/form-data" class="formulaire" method="get" action="../controllers/import_controller.php">

                    <table class="table">
                    <tr bgcolor="#f39c12">
                        <th align="center" width="33%">CATEGORIE</th>
                        <th align="center" width="33%">SOUS CATEGORIE</th>
                    </tr>

                    <tr>
                      <td><select type="text" name="categorie" placeholder="Categorie" onchange="liste_sous_categorie(this.value)" required/><?php liste_categorie(); ?></select></td>
                      <td><div id="liste_sous_categorie"><select required/></select></div></td>
                    </tr>
                    </table>

                    <table  id="image_add" class="table">

                      <tr>
                      <td><span>LIBELLE</span></td>
                      <td><input type="text" name="libelle" required/></td>
                      </tr>
                      <tr>
                      <td><span>DESCRIPTION</span></td>
                      <td><textarea name="description" required></textarea></td>
                      </tr>
                      <tr>
                      <td><span>PRIX</span></td>
                      <td><input min="1" type="number" name="prix" required/></td>
                      </tr>
                      <tr>
                      <td><span>STOCK</span></td>
                      <td><input min="1" type="number" name="stock" required/></td>
                      </tr>

                      <tr>
                      <td><span>IMAGE 1</span></td>
                      <td><input type="file" name="image1" accept="image/*" required/></td>
                      </tr>

                    </table>

                    <input type="hidden" value="1" name="nbre_image_article" id="nbre_image_article">
                    <input type="hidden" value="1" name="article_add">

                    <center><input align="center" type="submit" value="VALIDER" class="btn btn-primary" style="margin-right: 5px;"></input><i class="fa fa-task"></i></center>
                    <br>
                 </form> 

And the PHP code

if(isset($_GET['article_add']))
{
$libelle=addslashes(htmlentities($_GET['libelle']));
$description=addslashes(htmlentities($_GET['description']));
$prix=$_GET['prix'];
$stock=$_GET['stock'];
$sous_categorie_id_sous_categorie=$_GET['sous_categorie'];

$nbre_image_article=$_GET['nbre_image_article'];

session_start();

$uploaddir = '../../views/images/articles/';
$uploadfile = $uploaddir . basename($_FILES['image1']['name']);
echo $_FILES['image1']['error'];
echo '<pre>';
if (move_uploaded_file($_FILES['image1']['tmp_name'], $uploadfile)) {
    echo "Le fichier est valide, et a été téléchargé
           avec succès. Voici plus d'informations :\n";
} else {
    echo "Attaque potentielle par téléchargement de fichiers.
          Voici plus d'informations :\n";
}

}

1 Answers1

1

Uploading files requires that a POST method be used, not a GET.

Therefore, you need to change your form's method to that method="post".

You will also need to change all instances of $_GET to $_POST.

  • A quick find & replace in your favorite editor, will change those in a jiffy.

Not doing that, you will be faced with more undefined index notices.

Reference:

You will also need to make sure that the folder you are attempting to upload to, has the proper permissions set for it.

Reference:

Or, modify the folder's permissions via FTP.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.


Foonotes:

I noticed you are using session_start();.

Make sure you are not outputting before header, should there contain any output above that.

It's best to place it at the top of your (full) code.

Should you see a headers sent notice, you can consult the following on Stack:

If you're not using any $_SESSION arrays (which there are none in your posted code), then you can safely remove session_start(); from your code.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Just for the record, I did not rely on comments for the answer I submitted. As some of you may know me, it's not my style. – Funk Forty Niner Feb 05 '16 at 18:44
  • I tried with the post method, I notice that I get 2 same form balise in my code , that's why the file was not uploaded Thank you – Ramiz BARRO Feb 07 '16 at 04:08