DAY-2 Source code fix  upload_logo

DAY-2 Source code fix upload_logo

proposal for ci4 upgrade code to a stable version, manage upload_logo // IN /root/app/Controllers/Config.php
Categories

DAY 2
thanks @owlbrudder

following explanation:
updating the company logo in its original version works but generates two critical errors in php 8.2 -- php8.3
let me explain ho, it all starts with the info_config.php form

when validating "change image" on POST to controller /root/app/Controllers/Config.php postSaveInfo method

  1. $upload_success = !empty($upload_data['error']); the variable $upload_success is not loaded ( !isset ) and causes a critical error on line 357 $message = $upload_success ? $message: strip_tags($upload_data['error']);
    fix in upload_logo method.
  2. // Method  upload_logo()  infos: if $file->guessExtension() is loaded after move function then not work ( /tmp/file_tmp is moved )

coded


	private function upload_logo(): array
	{
		helper(['form']);
		$validation_rule = [
			'company_logo' => [
				'label' => 'Company logo',
				'rules' => [
					'uploaded[company_logo]',
					'is_image[company_logo]',
					'max_size[company_logo,1024]',
					'mime_in[company_logo,image/png,image/jpg,image/gif]',
					'ext_in[company_logo,png,jpg,gif]',
					'max_dims[company_logo,800,680]',
				]
			]
		];

		if (!$this->validate($validation_rule))
		{
			return (['error' => $this->validator->getError('company_logo')]);
		}
		else
		{
			$file = $this->request->getFile('company_logo');

			$file_info = [
				'orig_name' => $file->getClientName(),
				'raw_name' => $file->getName(),
				'file_ext' => $file->guessExtension(),
				'error'	   => ''
			];

			 if (! $file->hasMoved()) {
				 $file->move(FCPATH.'uploads/');
				 $success = $this->appconfig->save(['company_logo' => $file->getClientName()]);
			}

			return ($file_info);
		}
	}
 

the $file_info = [...] block is moved up and an 'error' key is added to initialize the value to empty.
I hope this will be clear enough because I'm doing it with what I have.
bye see you tomorrow
PS // INFOS: https://stackoverflow.com/questions/58728648/how-to-display-uploaded-images-in-codeigniter-4

Comments

Related posts

 DAY-1 Source code fix remove_logo

DAY-1 Source code fix remove_logo

proposal for ci4 upgrade code to a stable version, manage remove_logo // IN /root/app/Controllers/Config.php
DAY-3 various code fixes, critical error

DAY-3 various code fixes, critical error

Various fixes to apply causing a critical error