0

I have an application where people can post bugs. They can add screenshots, the only thing is, everything gets written in my database, but the images won't save, I only see the number of the image. In the map 'uploads' are no images saved.

This is the error:

Notice: Undefined index: upload in /Applications/MAMP/htdocs/2IMD_KellyVerhaegen_Project2013/loggedIn.php on line 29
Notice: Undefined index: upload in /Applications/MAMP/htdocs/2IMD_KellyVerhaegen_Project2013/loggedIn.php on line 30
Notice: Undefined index: upload in /Applications/MAMP/htdocs/2IMD_KellyVerhaegen_Project2013/loggedIn.php on line 32

Php code:

if (isset($_POST['btnBug'])) 
{
// wanneer er op de knop geklikt is proberen we user te saven in de databank
if (!empty($_POST['btnBug'])) 
{
    /* controleren of titel en beschrijving velden zijn ingevuld */
    if(!empty($_POST['subject']) && !empty($_POST['post']))
    {
        /* zien of de bug kan gesaved worden */
    try
    {
            //nieuwe bug aanmaken en gegevens wegschrijven
            $bug = new Bug();
            $screenshot = time() . $_FILES['upload']['name'];
            move_uploaded_file($_FILES['upload']['tmp_name'],
            "uploads/" . $screenshot);
            $bug->Bug_Name  = $_FILES['upload']['name'];
            $bug->Screenshot = $screenshot;
            $bug->Post = htmlspecialchars($_POST['post']);
            $bug->Subject = htmlspecialchars($_POST['subject']);
            //kijken of de bug solved/unsolved is
            $keuze = $_POST['myradio'];
                if ($keuze == "Unsolved") {
                    $bug -> Status = "Unsolved";
                } else {
                    $bug -> Status = "Solved";
                }
            $bug -> User_id = htmlspecialchars($_POST['getUser']);
            $bug -> Project_id = htmlspecialchars($_POST['getProject']);
            // bug wordt gesaved
            $bug->saveBug();    
            }
        catch(Exception $e)
        {
            $feedback = $e->getMessage();
            echo "Vul alle velden in!"; 
        }
    }
}
}

My form:

<div id="bugform">
        <div class="control-group">
        <h4>Vul hier u bug in.</h4>
        <br />
            <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" class="form-horizontal">
                <label class="control-label" for="inputOnderwerp">Onderwerp</label> 
            <div class="controls">
                <input type="text" name="subject" />
            </div>
        <br />
            <label class="control-label" for="inputBeschrijving">Beschrijving</label>
            <div class="controls">
                <textarea name="post" name="post"></textarea>
            </div>
        <br />
            <label class="control-label" for="inputUnsolved">Unsolved</label>
            <div class="controls">
                <input type="radio" name="myradio" value="Unsolved" id="Unsolved" checked="true">
            </div>
        <br />
            <label class="control-label" for="inputSolved">Solved</label>
            <div class="controls">
                <input type="radio" name="myradio" value="Solved" id="Solved">
            </div>
            <?php
            if(isset($_POST['btnBug']))
            {
                echo "<img src='uploads/".$screenshot."'/>";

            }
            ?>
            <input type="file" name="upload" id="upload"  />
        <br />
        <br />
        <br />
    <!-- projects ophalen -->
        <label class="control-label" for="inputProjectKiezen">Project kiezen</label>
        <div class="controls">
        <?php
            if(mysqli_num_rows($allProjects) > 0)
            {
                echo "<select name= getProject>";   
                while ($row = mysqli_fetch_assoc($allProjects)) 
                {
                    echo "<option value=" . $row['project_id'] . ">" . $row['project_name'] . "</option>";
                }
                echo "</select>";
            }
            ?>
        </div>
        <br />
        <!-- users ophalen -->
        <label class="control-label" for="inputUserToekennen">Aan welke user toekennen?</label>
        <div class="controls">
        <?php
            if(mysqli_num_rows($showUser) > 0)
            {
                echo "<select name= getUser>";  
                while ($row = mysqli_fetch_assoc($showUser)) 
                {
                    echo "<option value=" . $row['user_id'] . ">" . $row['username'] . "</option>";
                }
                echo "</select>";
            }
        ?>
        </div>
        <br />
        <div class="controls">
            <input class="btn btn-info dropdown-toggle" type="submit" name="btnBug" id="btnBug" value="Verzenden" />    
        </div>
        </form>
    </div>

I've been searching for a whole day, can somebody please help me? Thanks.

2
  • upload_max_filesize in your php.ini - put in a higher value. Commented Aug 20, 2013 at 20:44
  • 1
    enctype="multipart/form-data" in your form Commented Aug 20, 2013 at 20:48

1 Answer 1

1

You need to add the enctype="multipart/form-data" attribute to the <form> element.

The reason for this is that the default enctype (which is how data is encoded) is application/x-www-form-urlencoded, which does not allow the encoding of entire files.

Sign up to request clarification or add additional context in comments.

2 Comments

You should also probably check that upload exists with array_key_exists before referencing it, that way you won't get notices in the logs.
Thank you @Explosion Pills, Robert Rozas had already commented and it's working now!

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.