0

I am trying to upload an image file to my server using some code I found on the internet but it doesn't work and I can't find out why.

  <form method="post" action="?p=edit-profil&id=<?php echo($user_id); ?>">

        <h2>Profil bearbeiten</h2>
        <hr />

        <h4>Profilbild ändern</h4>
        <p style="margin-top: 5px;">(Zulässige Dateiformate: .png .jpg)</p>
        <input type="hidden" name="size" value="1000000" style="padding: 6px;">
        <div>
          <input type="file" name="image" class="choose-image">
        </div>

        <div>
          <input type="submit" name="add-personal-data" value="Speichern" class="submitbutton">
        </div>

      </form>
// Get image name
    $image = $_FILES['image']['name'];

    // image file directory
    $target = "img/".basename($image);

    $image_insert = 'img/'.$image;

    $image_upload_statement = $pdo->prepare("UPDATE beschreibung SET profilbild = '$image_insert' WHERE user_id = '$user_id'");
    $image_upload_statement->execute();

    if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
      $msg = "Image uploaded successfully";
    }else{
      $msg = "Failed to upload image";
    }

So usually image_insert should be someting like img/picture.png but I only get img/ and there is no upload aswell. Since I am quit new with working with this topic I have no more idea how to fix the problem.

CptK
  • 51
  • 1
  • 7

1 Answers1

0

Your form tag should have this attribute enctype="multipart/form-data" to be able to send the image to the php file.