0

I am getting the error that my index is undefined in $_FILES. I am trying to copy a picture with php on my server.

Error:

PHP Notice:  Undefined index: bilddatei in C:\inetpub\wwwroot\aufgabe8.php on line 85
PHP Notice:  Undefined variable: namefile in C:\inetpub\wwwroot\aufgabe8.php on line 86

I've already tried with isset($_FILES["bilddatei"]) but this returns false too.

Code:

<?php
session_start();

if (!isset($_SESSION['Datenbank']) and
    !isset($_SESSION['Kennung']) and !isset($_SESSION['Passwort']) )
{
    echo "<p>Bitte starten Sie zuerst mit der Eingabe-Startseite. Sie werden automatisch nach 5 Sekunden weitergeleitet.</p>";
    // automatisches Zurueckspringen nach 5 Sekunden nach 'index.html': 
    echo '<meta http-equiv="Refresh" content="5;url=login.php">';
} else {
          echo "<form action=\"aufgabe8.php\" method=\"post\" enctype=\"multipart/form-data\">
                    <p>Datei von PC hochladen
                        <input type=\"file\" name=\"bilddatei\" size\"60\" accept=\"image/*\"/>
                    </p>

                    <p><input type=\"submit\" value=\"Bild anzeigen\" name=\"bildBtn\"/></p>
                </form>";


    if(isset($_POST["bildBtn"])) {
            echo "<p>BILD IF</p>";

            $nameFile = $_FILES['bilddatei']['name'];
            echo "<p>$namefile</p>";

            //copy($fileSource,$destination); 

    }

Do you have any ideas? Thanks! :-)

3
  • Is this a typo that your else is not closed? Commented Jun 28, 2017 at 12:48
  • just because you echo the form, doesn't mean it's submitted. the code is confusing as to weather you think this or the form is displayed on submission. Commented Jun 28, 2017 at 12:57
  • Nope the else is closed I've just copied the relevant code Commented Jun 28, 2017 at 13:06

2 Answers 2

2

Variable name is different. Both php variable is case-sensitive.check $nameFile <=> $namefile.

https://www.w3schools.com/php/php_variables.asp

$namefile = $_FILES['bilddatei']['name'];
echo "<p>$namefile</p>";
Sign up to request clarification or add additional context in comments.

1 Comment

you have to take care about variable definition.
1

I think these things need to be done separately instead...

Render form

if (!isset($_SESSION['Datenbank']) and
    !isset($_SESSION['Kennung']) and !isset($_SESSION['Passwort']) )
{
    echo "<p>Bitte starten Sie zuerst mit der Eingabe-Startseite. Sie werden automatisch nach 5 Sekunden weitergeleitet.</p>";
    // automatisches Zurueckspringen nach 5 Sekunden nach 'index.html': 
    echo '<meta http-equiv="Refresh" content="5;url=login.php">';
} else {
    echo "<form action=\"aufgabe8.php\" method=\"post\" enctype=\"multipart/form-data\">
            <p>Datei von PC hochladen
                <input type=\"file\" name=\"bilddatei\" size\"60\" accept=\"image/*\"/>
            </p>

            <p><input type=\"submit\" value=\"Bild anzeigen\" name=\"bildBtn\"/></p>
        </form>";
}  ///missing } close I am assuming it goes here

Second php file ( say aufgabe8.php to match the form action )

if(isset($_POST["bildBtn"])) {
    echo "<p>BILD IF</p>";

    $nameFile = $_FILES['bilddatei']['name'];
    echo "<p>$nameFile</p>";  //spelling  $nameFile is not $namefile

    //copy($fileSource,$destination); 

}

Just because you echo out the form is very different then when it's submitted. The code is confusing on whether or not you understand this. In any case they are 2 distinct actions.

Besides the spelling mistakes as mentioned in the other answer.

Now you can do them in the same page like this

<?php
session_start();

if (!isset($_SESSION['Datenbank']) and
    !isset($_SESSION['Kennung']) and !isset($_SESSION['Passwort']) )
{
    echo "<p>Bitte starten Sie zuerst mit der Eingabe-Startseite. Sie werden automatisch nach 5 Sekunden weitergeleitet.</p>";
    // automatisches Zurueckspringen nach 5 Sekunden nach 'index.html': 
    echo '<meta http-equiv="Refresh" content="5;url=login.php">';
} else {

    if(isset($_POST["bildBtn"])) {
        ///submitted so process it
        echo "<p>BILD IF</p>";

        $nameFile = $_FILES['bilddatei']['name'];
        echo "<p>$nameFile</p>";  //spelling  $nameFile is not $namefile

        //copy($fileSource,$destination); 
    }else{
        ///not submitted so rendor the form
        echo "<form action=\"aufgabe8.php\" method=\"post\" enctype=\"multipart/form-data\">
            <p>Datei von PC hochladen
                <input type=\"file\" name=\"bilddatei\" size\"60\" accept=\"image/*\"/>
            </p>

            <p><input type=\"submit\" value=\"Bild anzeigen\" name=\"bildBtn\"/></p>
        </form>";

    }
}

You can even render the form, and process it in the same block ( without the else ), but just rendering it doesn't mean someone submitted it. In this case I would do away with the echo and just us plain old html to output the form.

3 Comments

So you mean I have to do it in two separate files? Because normally I do everything in one file and check if a button is clicked or a variable is set with isset.
Technically, you can do it in the same file, but just echoing the form, is not the same as submitting it. Echoing it out, is a sever operation, and submitting it is something the client does.
In other words, you can output the form, but until a user submits it you can't process the data in it, right. With that missing } its hard to tell if you were making that mistake or not. For ease of coding and readabillity, it's often better to handle them separately, then you can avoid having a condition to either render or process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.