SilenceIsGolden
SilenceIsGolden

Reputation: 59

Upload image to database Undefined index

I made theform working with storing the images directly on database and now, I want to learn how to store them in a folder, and store in database just the path.

At this moment I get this error Notice: Undefined index: uploaded_file in and I really don't understand why. Please, some F1 :) Html form:

<form action="ad_cont.php" method="POST" class="add_contact" name="add_contact" enctype="multipart/form-data">
<input type="file" name="uploaded_file" multiple required> 
<input type="submit" value="Upload"  class="button">  
</form>

Php script:

$img_path = "images/avatar";
$img_path = $img_path . basename( $_FILES['uploaded_file']['name']);

if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $img_path)) {
   mysqli_query($conn,"INSERT INTO `uploads` (filename,path)
   VALUES ('".$_FILES['uploaded_file']['tmp_name']."','".$img_path."')");

Upvotes: 0

Views: 227

Answers (2)

AeJey
AeJey

Reputation: 1457

You have to add enctype='multipart/form-data' to your form for file uploads to work.

<form action="ad_cont.php" method="POST" class="add_contact" enctype="multipart/form-data" name="add_contact">
<input type="file" name="uploaded_file" multiple required> 
<input type="submit" value="Upload"  class="button">  
</form>

The change php code like this.

$img_path = "images/avatar";
$img_path = $img_path . basename( $_FILES['uploaded_file']['name']);

$img_name= $_FILES['uploaded_file']['tmp_name'];
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $img_path)) {
   mysqli_query($conn,"INSERT INTO `uploads` (filename,path)
   VALUES ('".$img_name."','".$img_path."')");

Try changing the code of your form like this and hope it will work then.

Upvotes: 3

Ravi Dhoriya ツ
Ravi Dhoriya ツ

Reputation: 4414

To upload file to server, you need to mention enctype='multipart/form-data' in the <form>

<form enctype="multipart/form-data" action="ad_cont.php" method="POST" class="add_contact" name="add_contact" >
..
..
</form>

Upvotes: 0

Related Questions