1

I am trying to upload an image to be stored in a database on mysql however I keep receiving an error:

Notice: Undefined index: profilepic in >[php-path]/tuto>rsignupsubmit.php on line 17

There is a simple button upload of type file on a form:

<div class="field">
    <label>Profile Picture</label>
    <input class="button" type="file" name="profilepic">
</div>

I then have this code on another submit page, I am quite new to PHP however I have searched and searched and cannot find a solution anywhere.

//File upload
$target_dir = "../img/";
$newprofilepic = $target_dir . basename($_FILES ["profilepic"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($newprofilepic,PATHINFO_EXTENSION));

$newlocation = mysqli_real_escape_string($conn,$_POST['location']);
$insertquery = "INSERT INTO tutors(name, username, password, email, mobile, 
profilepic, location, 
message)"."VALUES('$newname','$newusername','$newpassword','$newemail', 
'$newmobile', '$newprofilepic', '$newlocation', '$newmessage')";

$result = mysqli_query($conn, $insertquery) or die(mysqli_error($conn));


mysqli_close($conn);

Thanks in advance.

2
  • I suggest you to do var_dump($_FILES); in order to see what exactly your server is receiving. Commented Mar 20, 2018 at 3:22
  • can you share your whole html code?? Commented Mar 20, 2018 at 6:58

2 Answers 2

1

I've tried to reproduce this error. Here is my code

<form action="" method="post" enctype="multipart/form-data" >
   <div class="field">
      <label>Profile Picture</label>
      <input class="button" type="file" name="profilepic">
   </div>
   <div>
      <input type="submit">
   </div>
</form>

$target_dir = "../img/";
$newprofilepic = $target_dir . basename($_FILES["profilepic"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($newprofilepic, PATHINFO_EXTENSION));

And this error doesn't appear for me. Are you using enctype="multipar/form-data" in your form tag?

This error can appear if this key doesn't exist in your $_FILES array. Please check it using var_dump($_FILES);

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

2 Comments

I've now got the string name uploading to a mysql table however I am having issues with uploading the actual image to the server so that it can be displayed correctly from the table onto the website?
How are you put file to the server? Using copy() method? Just make copy($_FILES['profilepic']['tmp_name'], 'your_file_name.jpg') check if everything were good, only then add new row to the database.
0

Undefined index: is a common error when you are calling an element of an array or object but that element does not exist. For example if in the below $_FILES array there is no "profilepic" element you would get this error.

$_FILES ["profilepic"]["name"]

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.