1

I am trying to upload an image. I have included the form from a different page (inc/pages/addRecipe2.php) and the form loads fine and I am running the code for the form in the same page (addRecipe/?token=...), when I try to upload the file it says that there is an undefined index: image

the errors I am getting: enter image description here

form code:

<form enctype="multipart/file-data" method="POST">
        <div class="form-group">
            <input type="file" class="form-control-file" id="image" name="image">
        </div>
        <?php
        if ($privateRecipe == "public") {
            echo "<button type='submit' class='btn btn-primary' style='width: 100%' id='finishRecipe' name='finishRecipe'>Submit recipe for review</button>";
        } else {
            echo "<button type='submit' class='btn btn-primary' style='width: 100%' id='finishRecipe' name='finishRecipe'>Complete reci</button>";
        }
        ?>
        <p class="formMessage1"></p>
    </form>

php code, running the form:

if (isset($_POST['finishRecipe'])) {
$recipeId = $_GET['token'];
$image = $_FILES['image'];

$imageName = $_FILES['image']['name'];
$imageTmpName = $_FILES['image']['tmp_name'];
$imageSize = $_FILES['image']['size'];
$imageError = $_FILES['image']['error'];
$imageType = $_FILES['image']['type'];
$fileExt = explode(".", $imageName);
$fileActualExt = strtolower(end($fileExt));
$allowedExt = array('png', 'jpg', 'jpeg');


if (in_array($fileActualExt, $allowedExt)) {
    if ($imageError === 0) {
        if ($imageSize < 500000) {
            $fileNameNew = uniqid('', true) . "." . $fileActualExt;
            $fileDestination = "uploads/$fileNameNew";
            move_uploaded_file($imageTmpName, $fileDestination);
        } else {
            echo "The image is too big!";
        }
    } else {
        echo "There was an error with your image!";
    }
} else {
    echo "File type not allowed!";
}

}

it does echo out "file type not allowed" as one of my error messages I made.

Any solutions?

9
  • The extension of the file you've uploaded is not 'png', 'jpg' or 'jpeg' ? $allowedExt = array('png', 'jpg', 'jpeg'); Commented Oct 22, 2019 at 5:58
  • what is the value of strtolower( end( $fileExt ) ) Commented Oct 22, 2019 at 6:01
  • I have updated the post with the errors I am getting - in the image Commented Oct 22, 2019 at 6:03
  • Possible duplicate of file upload php $_FILES undefined index error Commented Oct 22, 2019 at 6:10
  • 2
    change enctype of form enctype="multipart/form-data"> Commented Oct 22, 2019 at 6:17

2 Answers 2

2

first change the enctype to form-data

<form enctype="multipart/form-data" method="POST">

and then check

if (isset($_FILES["image"]["name"])) {
        if (in_array($fileActualExt, $allowedExt)) {
            if ($imageError === 0) {
                if ($imageSize < 500000) {
                    $fileNameNew = uniqid('', true) . "." . $fileActualExt;
                    $fileDestination = "uploads/$fileNameNew";
                    move_uploaded_file($imageTmpName, $fileDestination);
                } else {
                    echo "The image is too big!";
                }
            } else {
                echo "There was an error with your image!";
            }
    } else {
        echo "File type not allowed!";
    }

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

Comments

0

The issue is generated may be your file size bigger than default max_upload size.

So please change it in your php.ini:

`upload_max_filesize` = 128M

or other option to resolve this error is check that image name set or not.

    if (isset($_FILES["image"]["name"])) {

        $name = $_FILES["file"]["name"];
        $tmp_name = $_FILES['file']['tmp_name'];
        $error = $_FILES['file']['error'];

        if (!empty($name)) {
           $location = 'uploads/';
           if  (move_uploaded_file($tmp_name, $location.$name)){
               echo 'Uploaded';
            }

        } else {
          echo 'please choose a file';
        }
    }

5 Comments

what I mean is, it says that "image" is undefined, that's the error I am getting. how do I fix that?
@ethan patwell did you try with 1st solution?
Yes, same error, the error is undefined index: image
the form is on a different page, which I have included in the main file
can you change form attribute enctype="multipart/file-data" to enctype="multipart/form-data"?

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.