Hi,
I am uploading a file using cakphp4 “File uploads Function”. But i will give me this error " Object of class Laminas\Diactoros\UploadedFile could not be converted to string [ CORE/src/Database/Type/StringType.php , line 97 ]".
Controller code
public function add()
{
$undertaker = $this->Undertakers->newEmptyEntity();
if ($this->request->is(‘post’)) {
$data = $this->request->getData();
$logo = $data[‘logo’];
$undertaker = $this->Undertakers->patchEntity($undertaker, $data);
if(!$undertaker->getErrors()) {
$name = $logo->getClientFilename();//uploads/undertakers/logo/1/f30db346571d4bc99f1875e6f4cbf840d0e61c79.jpg
$ext = pathinfo($name, PATHINFO_EXTENSION);
$folder = Text::uuid();
$fileName = Text::uuid();
$dir = new Folder(env(‘TEMPFILE_URL’, WWW_ROOT . ‘uploads’).DS .‘undertakers’.DS.‘logo’. DS . $folder, true, 0755);
$savepath = $dir->path .DS . $fileName.’.’.$ext;
$logo->moveTo($savepath);
$undertaker->logo = ‘uploads/undertakers/logo/’.$folder.’/’.$fileName.’.’.$ext;
}
if ($this->Undertakers->save($undertaker)) {
$this->Flash->success(__('The undertaker has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The undertaker could not be saved. Please, try again.'));
}
$this->set(compact('undertaker'));
}