0

I made a function to upload an image to my database and upload the image to a folder.

The url in the database changes to the url of the image but the file does not upload to the folder.

I get the error: Undefined index: user_image in image.php

Here is my code:

Image.php

<?php
$edit_row['opzoekImage'] = $_POST["user_image"];
$imgFile = $_FILES['user_image']['name'];
$tmp_dir = $_FILES['user_image']['tmp_name'];
$imgSize = $_FILES['user_image']['size'];

if($imgFile)
{
    $upload_dir = 'user_images/'; // upload directory   
    $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
    $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
    $userpic = rand(1000,1000000).".".$imgExt;
    if(in_array($imgExt, $valid_extensions))
    {           
        if($imgSize < 5000000)
        {
            unlink($upload_dir.$edit_row['opzoekImage']);
            move_uploaded_file($tmp_dir,$upload_dir.$userpic);
        }
        else
        {
            $errMSG = "Sorry, your file is too large it should be less then 5MB";
        }
    }
    else
    {
        $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";        
    }   
}
else
{
    $userpic = $edit_row['opzoekImage']; // old image from database
}
?>

New.php

<?php 
    if(isset($_POST["submit"]) && isset($_GET['website_naam']) && isset($_GET['tbl_name']) && $_GET['tbl_name'] == "tblOpzoek") {
    include('config.php');
    $website_naam = $_GET['website_naam'];
    $sqlWebsiteId =  "SELECT websiteId FROM tblWebsite WHERE websiteNaam = '$website_naam'";
    if($db->query($sqlWebsiteId) != "") {
        $resultWebsiteId = $db->query($sqlWebsiteId);
        if ($resultWebsiteId->num_rows > 0) {
            // output data of each row
            while($row = $resultWebsiteId->fetch_assoc()) {
                $websiteId = $row["websiteId"];
            }
            include('image.php');
    $sqlToevoegenType = "INSERT INTO tblOpzoek (websiteId, opzoekName, opzoekValue, opzoekImage) VALUES ('".$websiteId."', '".$_POST["typeNaamToevoegen"]."', '".$_POST["typeWaardeToevoegen"]."', '".$userpic."')";
    if($db->query($sqlToevoegenType) === TRUE) {
        header('Location: ' . $_SERVER['HTTP_REFERER']);
        } else {
        echo "<script type= 'text/javascript'>alert('Error: " . $sqlToevoegenType . "<br>" . $db->error."');</script>";
        }
        } else {
            echo "0 results";
        }
        } 
    }
?>

My form: Good to notice I do have more forms with file input name="user_image"

<!-- Type toevoegen-->
<div class="modal fade" id="addType" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
     <form action="php/new.php?website_naam=<?php echo $websiteNaam ?>&tbl_name=tblOpzoek" method="post" id="formTypeToevoegen">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Type toevoegen</h4>
      </div>
      <div class="modal-body">
        <div class="form-group">
            <label>Type Naam</label>
            <input type="text" id="typeNaamToevoegen" name="typeNaamToevoegen" class="form-control" >
        </div>
        <div class="form-group">
            <label>Type Waarde</label>
            <input type="text" id="typeWaardeToevoegen" name="typeWaardeToevoegen" class="form-control" >
        </div>
        <div class="form-group">
            <label>Type Afbeelding</label>
            <input class="input-group" type="file" id="videoUploadFile" name="user_image" accept="image/*" />
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Sluiten</button>
        <button type="submit" name="submit" class="btn btn-primary">Gegevens opslaan</button>
      </div>
    </form>
    </div>
  </div>
</div>

Thanks for your time!

11
  • mention the error you are getting. Commented Apr 10, 2018 at 7:04
  • Do you get some errors or a stacktrace? Commented Apr 10, 2018 at 7:04
  • Sorry, check my updated answer please. Commented Apr 10, 2018 at 7:07
  • Is it user_image or user_images? Commented Apr 10, 2018 at 7:08
  • @proofzy I am getting user_image from my form. Commented Apr 10, 2018 at 7:10

2 Answers 2

4

You need to add the enctype attribute to your <form> tag

<form enctype="multipart/form-data">
Sign up to request clarification or add additional context in comments.

Comments

0

As per your error first check what name you define in input tag because if you define different name in input tag and define different name in $_FILES then it shows error.

So define same name in input tag and $_FILES e.g.

$_FILES['user_image']['name'];

If above are correct then check whether you add enctype="multipart/form-data" in form or not.

Comments

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.