Have to upload the image twice because the edit template doesn't show the image that was uploaded at the add page

It’s set in php.ini Maybe I will have to modify it.

I tried using a much smaller image and it didn’t fix the problem so that’s not it.

Please change your Validator to:

        $validator
            ->allowEmptyFile('home_page_image')
            ->add('home_page_image','file',[
              'rule'=>['mimeType',['image/gif','image/png','image/jpg','image/jpeg']],
              'message'=>__('upload only gif/jpg/png')
            ]);

Then in your Controller in the edit Method:

$home_page_image = $this->request->getData('home_page_image');
//check if $home_page_image has no errors or is not empty
if ($home_page_image->getError() == 0) {
                      $imageFilename = $home_page_image->getClientFilename();
                      $bio->resize_to_thumb='img/bio_images/'.$imageFilename;
                      $destination = WWW_ROOT.'img'.DS.'bio_images'.DS.$imageFilename;
                      $home_page_image->moveTo($destination);
                    }

It didn’t work.
In the edit method of the controller, I have the following before your call to getError():

if (!empty($this->request->getData('home_page_image'))) {
  //$home_page_image = $this->request->getData('home_page_image');  //this line throws an error
  $home_page_image = $this->request->getUploadedFile('home_page_image'); 
  if ($home_page_image->getError() == 0) {

For
$home_page_image = $this->request->getData('home_page_image');
I get the error
Call to a member function getError() on array
So I had to change it to
$home_page_image = $this->request->getUploadedFile('home_page_image');

I put a debug statement here:


if (!empty($this->request->getUploadedFile('home_page_image'))) {	  
$home_page_image = $this->request->getUploadedFile('home_page_image'); 
debug($home_page_image);

And here are the results of my debug:
APP/Controller\BiosController.php (line 220)
object(Laminas\Diactoros\UploadedFile) id:0 {
private clientFilename => ‘2.P12_id80604275.jpg’
private clientMediaType => ‘image/jpeg’
private error => (int) 0
private file => ‘C:\xampp\tmp\php6388.tmp’
private moved => false
private size => (int) 126356
private stream => object(Laminas\Diactoros\Stream) id:1 {
protected resource => (resource) Resource id #137
protected stream => ‘C:\xampp\tmp\php6388.tmp’
}
}

I looked at the environment tab and it showed the php.ini information. I haven’t exceeded the maximum file size.

From googling I found a possible reason for the images not being saved. The source said that ID Is supposed to have auto increment enabled and it is supposed to be the primary key. The ids in my bios table don’t auto increment but they are the primary keys. When a bio is created, I set the id equal to the user_id in the add method. It looks like this:

$bio->id = $user_id;
  if ($this->Bios->save($bio, 'id')) {	
    $this->Flash->success(__('The bio has been saved.'));
    return $this->redirect(['action' => 'index']);
  }

Auto incrementing the id doesn’t work because it has to be equal to the user id.

I looked at the cakephp documentation for version 4 and I saw that I could use getError() to debug but I don’t know how I’m supposed to call the variable name $errors
$errors = $bio->getError('home_page_image');

I know it’s already checking for errors here:
if ($home_page_image->getError() == 0)
but I wanted to check it again in a different part of the code.

Saving the bio id as the user id is a problem because when the bio is deleted and a new bio is created, the bio id can’t be used again but the user id remains the same. I don’t know how to solve this problem yet.

Use RDBMS concept

you should read this chapter
https://book.cakephp.org/4/en/orm/associations.html

and please post the thole edit part of your controller

public function edit($id = null) {
        $bio = $this->Bios->get($id, [
            'contain' => [],
        ]);
	
        if ($this->request->is(['patch', 'post', 'put'])) {
			$home_page_image = $this->request->getData('home_page_image');
			$bio -> home_page_image = $home_page_image;
            $bio = $this->Bios->patchEntity($bio, $this->request->getData());
		
          if(!$bio->getErrors()) {

            if (!empty($this->request->getData('resize_to_thumb'))) {
		      $resize_to_thumb=$this->request->getUploadedFile('resize_to_thumb'); 
              if ($resize_to_thumb->getError() == 0) {
                $destination = WWW_ROOT.'img'.DS.'bio_images'.DS.$resize_to_thumb->getClientFilename();
				$fileName = $resize_to_thumb->getClientFilename();
			    $fileName = 'img/bio_images/' . $fileName;
				$bio->resize_to_thumb=$fileName;
                $resize_to_thumb->moveTo($destination);
             }
          }
        if (!empty($this->request->getData('home_page_image'))) {  
				$fileName2 = $home_page_image->getClientFilename();
			    $destination2 = WWW_ROOT.'img'.DS.'bio_images'. DS . $fileName2;
			    $fileName2 = 'img/bio_images/' . $fileName2;
				$bio->home_page_image=$fileName2;
                $home_page_image->moveTo($destination2);
				
		}
        }
        }
        if ($this->Bios->save($bio)) {
          $this->Flash->success(__('The bio has been saved.'));
          return $this->redirect(['action' => 'index']);
        }
          $this->Flash->error(__('The bio could not be saved. Please, try again.'));
        }
        $users = $this->Bios->Users->find('list', ['limit' => 200]);
        $this->set(compact('bio', 'users'));
    }

It still doesn’t work. In my bios table I have:

 $validator
            ->allowEmptyFile('resize_to_thumb')
            ->add('resize_to_thumb','file',[
              'rule'=>['mimeType',['image/gif','image/png','image/jpg','image/jpeg']],
              'message'=>__('upload only gif/jpg/png')
            ]);
			
	   $validator
            ->allowEmptyFile('home_page_image')
	    //->isArray('home_page_image')
	    //->allowEmptyArray('home_page_image')
            ->add('home_page_image','file',[
              'rule'=>['mimeType',['image/gif','image/png','image/jpg','image/jpeg']],
              'message'=>__('upload only gif/jpg/png')
            ]);

I will pay $100 via paypal to whoever can fix this for me.

remove this line:
$bio->home_page_image = $home_page_image;

and then do
debug($bio);
after patchEntity

and show the result of the debu and also a debug of $this->request->getData().

I uploaded a photo but the debug showed that home_page_image is null:
The results of

$bio = $this->Bios->patchEntity($bio, $this->request->getData());
debug($bio);

object(App\Model\Entity\Bio) id:0 {
‘id’ => (int) 35
‘user_id’ => (int) 35
‘contributor’ => ‘Delete doesn’t work’
‘resize_to_thumb’ => null
‘home_page_image’ => null
‘body’ => ‘

delete failed


‘gender’ => ‘’
‘created’ => object(Cake\I18n\FrozenTime) id:1 { }
‘modified’ => object(Cake\I18n\FrozenTime) id:2 { }
‘[new]’ => false
‘[accessible]’ => [ ]
‘[dirty]’ => [ ]
‘[original]’ => [ ]
‘[virtual]’ => [ ]
‘[hasErrors]’ => false
‘[errors]’ => [ ]
‘[invalid]’ => [ ]
‘[repository]’ => ‘Bios’
protected _accessible => [ ]
protected _fields => [ ]
protected _original => [ ]
protected _hidden => [ ]
protected _virtual => [ ]
protected _dirty => [ ]
protected _accessors => [ ]
protected _new => false
protected _errors => [ ]
protected _invalid => [ ]
protected _registryAlias => ‘Bios’
}

The results of

$bio = $this->Bios->patchEntity($bio, $this->request->getData());
debug($this->request->getData());

on the other hand showed that an image had been uploaded:
‘user_id’ => ‘35’,
‘contributor’ => ‘Delete doesn’t work’,
‘gender’ => ‘’,
‘body’ => ‘

delete failed

’,
‘resize_to_thumb’ => [
‘tmp_name’ => ‘’,
‘error’ => (int) 4,
‘name’ => ‘’,
‘type’ => ‘’,
‘size’ => (int) 0,
],
‘home_page_image’ => [
‘tmp_name’ => ‘C:\xampp\tmp\php9179.tmp’,
‘error’ => (int) 0,
‘name’ => ‘2.P10_id80604273.jpg’,
‘type’ => ‘image/jpeg’,
‘size’ => (int) 117590,
],
]

what is the result of debug($bio) right before saving the entity?

This is where I save the entity:

if ($this->Bios->save($bio)) {
          $this->Flash->success(__('The bio has been saved.'));
          return $this->redirect(['action' => 'index']);
        }

Where should I put debug($bio)?

capture the result in a variable, then debug it, then test it:

$result = $this->Bios->save($bio);

debug($result);

if ($result) {
    $this->Flash->success(__('The bio has been saved.'));
    return $this->redirect(['action' => 'index']);
}