I’m trying to save my image to Drupal’s file_managed
table. However, my image is coming from base64 decode:
$filename = "sign-".$user->id(); // returns "sign-1234" $image_raw = $form_state->getValue('signature'); // returns base64 string of png $data = base64_decode(preg_replace('#^data:image/w+;base64,#i', '', $image_raw)); $image = file_put_contents('private://requestformfiles/'.$filename.'.png', $data); $file = File::load($image); // does not get anything $file->setPermanent(); // fails $file->save(); $result = $connection->insert('request_id') ->fields([ ... 'signature' => $image, ... ]) ->execute();
From what i see, My file successfully uploads into the private files folder, and image
returns a random 4 digit number. However, when setting it to file_managed
in the Database, it fails, since File::load
cannot see my $image
.
Any help is again, appreciated.