Hello,
I upload a picture in folder and i upload filename in database.
My problem :
Warning (4096): Object of class Laminas\Diactoros\UploadedFile could not be converted to string [CORE\src\Database\Type\StringType.php, line 97]
Warning (512): Unable to emit headers. Headers sent in file=C:\wamp64\www\GrowBook\vendor\cakephp\cakephp\src\Error\Debugger.php line=970 [CORE\src\Http\ResponseEmitter.php, line 71]
Warning (2): Cannot modify header information - headers already sent by (output started at C:\wamp64\www\GrowBook\vendor\cakephp\cakephp\src\Error\Debugger.php:970) [CORE\src\Http\ResponseEmitter.php, line 168]
Warning (2): Cannot modify header information - headers already sent by (output started at C:\wamp64\www\GrowBook\vendor\cakephp\cakephp\src\Error\Debugger.php:970) [CORE\src\Http\ResponseEmitter.php, line 197]
Warning (2): Cannot modify header information - headers already sent by (output started at C:\wamp64\www\GrowBook\vendor\cakephp\cakephp\src\Error\Debugger.php:970) [CORE\src\Http\ResponseEmitter.php, line 197]
Warning (2): Cannot modify header information - headers already sent by (output started at C:\wamp64\www\GrowBook\vendor\cakephp\cakephp\src\Error\Debugger.php:970) [CORE\src\Http\ResponseEmitter.php, line 197]
Cake\Database\Type\StringType::marshal() - CORE\src\Database\Type\StringType.php, line 97
Cake\ORM\Marshaller::Cake\ORM\{closure}() - CORE\src\ORM\Marshaller.php, line 79
Cake\ORM\Marshaller::merge() - CORE\src\ORM\Marshaller.php, line 573
Cake\ORM\Table::patchEntity() - CORE\src\ORM\Table.php, line 2854
App\Controller\SerresController::add() - APP/Controller\serresController.php, line 52
Cake\Controller\Controller::invokeAction() - CORE\src\Controller\Controller.php, line 531
Cake\Controller\ControllerFactory::invoke() - CORE\src\Controller\ControllerFactory.php, line 149
Cake\Http\BaseApplication::handle() - CORE\src\Http\BaseApplication.php, line 313
Cake\Http\Runner::handle() - CORE\src\Http\Runner.php, line 77
Cake\Http\Middleware\CsrfProtectionMiddleware::process() - CORE\src\Http\Middleware\CsrfProtectionMiddleware.php, line 169
Cake\Http\Runner::handle() - CORE\src\Http\Runner.php, line 73
Cake\Http\Middleware\BodyParserMiddleware::process() - CORE\src\Http\Middleware\BodyParserMiddleware.php, line 164
Cake\Http\Runner::handle() - CORE\src\Http\Runner.php, line 73
Cake\Routing\Middleware\RoutingMiddleware::process() - CORE\src\Routing\Middleware\RoutingMiddleware.php, line 161
Cake\Http\Runner::handle() - CORE\src\Http\Runner.php, line 73
Authentication\Middleware\AuthenticationMiddleware::process() - ROOT\vendor\cakephp\authentication\src\Middleware\AuthenticationMiddleware.php, line 124
My code :
public function add()
{
$serre = $this->Serres->newEmptyEntity();
if ($this->request->is('post')) {
$serre = $this->Serres->patchEntity($serre, $this->request->getData());
$image='';
$image = $this->request->getData('image');
if(isset($image)){
if(!$serre->getErrors()){
$all= $image->getClientFilename();
$explo = explode('.', $all);
$name = $explo[0].$_SESSION['Auth']['id'].'.'.$explo[1];
$targetPath = WWW_ROOT.'imgSerres'.DS.$name;
if($all){
$image->moveTo($targetPath);
}
$serre->image = $name;
}
}
if ($this->Serres->save($serre)) {
$this->Flash->success(__('The serre has been saved.'));
return $this->redirect(['action' => './index']);
}
$this->Flash->error(__('The serre could not be saved. Please, try again.'));
}
$this->set(compact('serre'));
}
Table code :
$validator
->allowEmptyFile('image')
->add('image', [
'mimeType' => [
'rule' => ['mimeType', ['image/jpg', 'image/png', 'image/jpeg']],
],
'fileSize' => [
'rule' => ['fileSize', '<=', '8MB'],
],
]);
Can you help me?
Thanks.