Hi. I hope that anyone can help me.
I have table pages that contain tow fileds images and cover.
I need to upload two pictures in one form, I see that in this version it is a bit difficult to deal with this error .
Object of class Laminas\Diactoros\UploadedFile could not be converted to a string
Question : how i can solve this problem and how i can allow images or cover to be empty
the table pages contain two fields (image and cover).
inside the PagesTable.php I added the following code.
$validator
->allowEmptyFile('image_file')
->add('image_file', [
'mimeType' => [
'rule' => ['mimeType', ['image/jpg', 'image/png', 'image/jpeg']],
'message' => 'Please upload only jpg and png.',
],
'fileSize' => [
'rule' => ['fileSize', '<=', '1MB'],
'message' => 'Image file size must be less than 1MB.',
],
]);
$validator
->allowEmptyFile('cover')
->add('cover', [
'mimeType' => [
'rule' => ['mimeType', ['image/jpg', 'image/png', 'image/jpeg']],
'message' => 'Please upload only jpg and png.',
],
'fileSize' => [
'rule' => ['fileSize', '<=', '1MB'],
'message' => 'Image file size must be less than 1MB.',
],
]);
return $validator;
Inside the view I addd the following Code:
<?= $this->Form->control( 'image_file', array( 'type' => 'file', 'label' => array( 'text' => __d('admin', 'Main Image'), 'class' => 'col-sm-2 control-label' ), 'class' => '', 'style' => 'width:83.3333%;margin-bottom:10px', 'placeholder' => __d('admin', 'Main Image'), 'required' ) ) ?><?= $this->Form->control(
'cover',
array(
'type' => 'file',
'label' => array(
'text' => __d('admin', 'Cover'),
'class' => 'col-sm-2 control-label'
),
'class' => '',
'style' => 'width:83.3333%;margin-bottom:10px',
'placeholder' => __d('admin', 'Cover')
)
) ?>
Inside the Controller i added the follwing Code:
if ($this->request->is([āpostā, āputā])) {
$page = $this->Pages->patchEntity($page, $this->request->getData(), ['translations' => true]);
if (!$page->errors) {
// the process of uploading image ///
if (isset($page->image_file) && !empty($page->image_file)) {
$fileReserved = $this->request->getData('image_file');
$name = $fileReserved->getClientFilename();
$prefixName = uniqid('image_', true);
$changName = $prefixName . $name;
if (!is_dir(WWW_ROOT . 'upload' . DS . 'pages')) {
mkdir(WWW_ROOT . 'upload' . DS . 'pages', 0775);
}
$targetPath = (WWW_ROOT . 'upload' . DS . 'pages' . DS . $changName);
if ($changName) {
$fileReserved->moveTo($targetPath);
}
$page->image = 'pages/' . $changName;
} else {
$page->image = 'pages/default_page.jpg';
}
// the process of uploading Cover ///
if (isset($page->cover) && !empty($page->cover)) {
$fileReserved = $this->request->getData('cover');
$name = $fileReserved->getClientFilename();
$prefixName = uniqid('image_', true);
$changName = $prefixName . $name;
if (!is_dir(WWW_ROOT . 'upload' . DS . 'pages')) {
mkdir(WWW_ROOT . 'upload' . DS . 'pages', 0775);
}
$targetPath = (WWW_ROOT . 'upload' . DS . 'pages' . DS . $changName);
if ($changName) {
$fileReserved->moveTo($targetPath);
}
$page->image = 'pages/' . $changName;
} else {
$page->image = 'pages/default_page.jpg';
}
}
}
Thanks for all