Upload picture in folder

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.

You have an image field in your database. It’s a string, for good reason. The incoming data has a field called image. It’s an object with various details about the image. It’s trying to patch that object into the string field, and failing. An easy solution might be to rename the image field in your form.

Hi,

Okay i try this, thanks

Hello,

I change fieldname ‘image’ to ‘image_file’ but this error is here again.

I don’t understand…

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_file');

            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_file = $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'));
    }
$validator
            ->allowEmptyFile('image_file')
            ->add('image_file',[ 
            'mimeType' => [
                'rule' => ['mimeType', ['image/jpg', 'image/png', 'image/jpeg']],
                'message' => "Le type de fichier n'est pas accepté",
            ],
            'fileSize' => [
                'rule' => ['fileSize', '<=', '8MB'],
                'message' => "L'image est trop lourde",
            ],
        ]);
<div class="addSerre">
    <h3 class="heading">Ajouter une serre</h3>
    <div class="container-fluid">
        <div class="serres form content">
            <?= $this->Form->create($serre, ['type' => 'file']) ?>
            <fieldset>
                <?php
                    $url = "";
                    echo $this->Form->control('nom', ['id' => 'nom', "class" => "nom"]);
                    echo $this->Form->control('type', ['id' => 'type', "class" => "nom"]);
                        ?><div class="file">
                        <div id="fileDisplayArea"></div> 
                        <label class="browse" for="imgSerre">Importer une image </label><?php
                          echo $this->Form->control('image_file', ['type' => 'file', 'label' => '', 'id' => 'imgSerre']);
                    ?> </div> <?php
                    echo $this->Form->control('id_Users', ['type' => 'hidden', 'value' => $_SESSION['Auth']['id']]);
                ?>
                <?= $this->Form->button('Ajouter') ?>

                <div class="back"><?= $this->Html->link("Revenir en arrière", ['action' => './index']) ?></div>
            </fieldset>
            <?= $this->Form->end() ?>
        </div>
    </div>
</div>

Can you help me again? please

Same error, or a different one?

Same error again

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]

it seems that you are getting the image as object and trying to save it in the database as string, use debug in the controller to check what $image holds.