-2

I want to upload a file from an input and rename it by the values of the inputs that the users provides but my script doesnt work and puts me these errors. My errors:

This is the HTML I am using:

    <div class="input-group mb-3">
      <div class="input-group-prepend">
        <label class="input-group-text" for="inputGroupSelect01" name="selectionClasse">Classe</label>
      </div>
      <select class="custom-select" id="inputGroupSelect01">
        <option selected disabled>Sélectionnez</option>
        <option value="6e">Sixième</option>
        <option value="5e">Cinquième</option>
        <option value="4e">Quatrième</option>
        <option value="3e">Troisième</option>
        <option value="2nd">Seconde</option>
        <option value="1e">Première</option>
        <option value="term">Terminale</option>
      </select>
    </div>
    <div class="input-group mb-3">
      <div class="input-group-prepend">
        <label class="input-group-text" for ="inputGroupSelect02">Matière</label>
      </div>
      <select class="custom-select" id="inputGroupSelect02" name="selctionMatiere">
        <option selected disabled>Sélectionnez</option>
        <option value="maths">Mathématiques</option>
        <option value="frc">Français</option>
        <option value="pc">Physique Chimie</option>
        <option value="angl">Anglais</option>
        <option value="svt">Sciences et Vie de la Terre</option>
        <option value="latin">Latin</option>
        <option value="esp">Espagnol</option>
      </select>
    </div>
    <input id="datepicker" width="250" name="choixCalendrierDate">
    <script>
        $("#datepicker").datepicker({
        uiLibrary: 'bootstrap4',
        viewMode: 'years',
        format: 'mm-yyyy',
        });
    </script>

This is the PHP script I am using to upload the files and rename them. The $nomfichier regroups all the values of the two options tags and the datepicker value. :

<?php

$folderDestination = '../uploads/img';  // ../ or ./ depends on your situation

$choixDeroulantClasse = isset($_POST['selectionClasse']);
$choixDeroulantMatiere = isset($_POST['selectionMatiere']);
$choixCalendrierDate = isset($_POST['choixCalendrierDate']);

$nomFichier = $choixDeroulantMatiere . $choixDeroulantClasse . $choixCalendrierDate;


if(isset($_POST['bouton']))
{
    $choixDeroulantClasse = $_POST['selectionClasse'];
    $choixDeroulantMatiere = $_POST['selectionMatiere'];
    $choixCalendrierDate = $_POST['choixCalendrierDate'];
    $nomFichier = $choixDeroulantMatiere . $choixDeroulantClasse . $choixCalendrierDate;
  if(file_exists($folderDestination)) 
  {
    if(is_writable($folderDestination)) {
        $files = array_filter($_FILES['files']['name']);
        $totalFiles = count($_FILES['files']['name']);
        $fileUploaded = array();
        for($i=0;$i<$totalFiles;$i++)
        {
            $fileName = $_FILES['files']['name'][$i];
            $ext = strtoupper(pathinfo($fileName, PATHINFO_EXTENSION));
            $fileDestination = $folderDestination.'/'.$fileName;
            if($ext == 'JPG' or $ext == 'PNG') 
            {
                move_uploaded_file($_FILES['files']['tmp_name'][$i], $folderDestination.$nomFichier);
                $fileUploaded[] = $fileName;
                // do echo                
                echo $fileName.' uploaded <span style="color: green; font-weight: bold;">correctly</span>';  
                // or header action
                header("Location: /submitfiled.php?info=uploadOK");                  
            } else { echo '<span style="color: red; font-weight: bold;">'. $fileName .' uploaded error</span>'; } 
            }
        } else { header("Location: /submitfail.php?info=folderNotExists"); }  
    } else { header("Location: /submitfail.php?info=folderIsNotWritable"); }
}

?>

I hope you can help me !

retr0
  • 11
  • 7
  • 2
    Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – El_Vanja Feb 17 '21 at 10:20
  • `$_POST['selectionMatiere']`, but `name="selctionMatiere"`. Then `name="selectionClasse"` is on the label instead of the actual form element (the select). – El_Vanja Feb 17 '21 at 10:22
  • Thank you I have no more errors now but the file that I want to upload doesn't appear on the /uploads/img folder – retr0 Feb 17 '21 at 10:23
  • You never actually check if `move_uploaded_file` succeeded. – El_Vanja Feb 17 '21 at 10:27

1 Answers1

0

Change

move_uploaded_file($_FILES['files']['tmp_name'][$i], $folderDestination.$nomFichier);

TO

move_uploaded_file($_FILES['files']['tmp_name'][$i], $folderDestination);

Piyush Shukla
  • 244
  • 1
  • 11