0

I trying to upload a photo in database, but it doesn't update and even upload it.

this is my php

if(isset($_POST['upload_photo'])){
// $profielfoto = $_POST['profielfoto_upload'];

$imagefileHandle = fopen($_FILES['profielfoto_upload']['name'], 'rb');
$imageData = fread($imagefileHandle, filesize($_FILES['profielfoto_upload']['tmp_name']));
fclose($imagefileHandle);

$query = $conn->prepare('UPDATE forum_inhoud SET profielfoto = :pf WHERE id = :id');
$query->execute([
    ':pf' => $imageData,
    ':id' => $_SESSION['user_id']
]);

$_SESSION['success'] = 'Uw gegevens zijn geupdate, U moet alleen opnieuw inloggen';
header("Location: ../../profile.php");
exit(0);
} 

and this is my html

<form action="app/instellingen/crud_instellingen.php" method="post">
    <div class="form-group row">
        <div class="col-sm-12">
            <input type="file" name="profielfoto_upload" class="form-control" ><!--accept="image/png,image/gif,image/jpeg,image/jpg"-->
        </div>
    </div>  
    <div class="form-group row text-center">
        <div class="col-sm-12">
            <button type="submit" name="upload_photo" class="btn btn-primary">Save Changes</button>
        </div>
    </div>
</form>

can you guys help me out?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Valdemar Vreeman
  • 177
  • 2
  • 16
  • 1
    add `enctype="multipart/form-data"` attribute to your form – Slava May 10 '19 at 14:27
  • Have a look at [the manual](https://www.php.net/manual/en/features.file-upload.post-method.php) about file uploads with PHP. There's no reason to use `fopen()` and manually read the contents to a new file. Just use `move_uploaded_file()`. The link above will show you some examples. – M. Eriksson May 10 '19 at 14:28
  • what does multipart/form-data stands for? – Valdemar Vreeman May 10 '19 at 14:29
  • @ValdemarVreeman Read link shared by Magnus. You will get it – Rahul May 10 '19 at 14:30
  • it is required form attribute for sending files to server – Slava May 10 '19 at 14:30
  • @ValdemarVreeman [answer](https://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean) – Rahul May 10 '19 at 14:31
  • @MagnusEriksson where do I add the move_uploaded_file() – Valdemar Vreeman May 10 '19 at 14:31
  • @ValdemarVreeman - `multipart/form-data` means that the form will send multiple types of data in the body. In this case, it will be your inputs and your file. Normally, it's just the inputs. If you don't have that enc-type, the server won't know that there is files as well and will ignore that data. – M. Eriksson May 10 '19 at 14:32
  • @ValdemarVreeman - Like I said in my comment, you should read the link to understand how it works, It also contains some examples. – M. Eriksson May 10 '19 at 14:32
  • @MagnusEriksson thank you it is working! – Valdemar Vreeman May 10 '19 at 14:35

0 Answers0