G
G
Gildars2016-08-01 00:34:20
PHP
Gildars, 2016-08-01 00:34:20

How to upload a file to the server using the Phalcon framework?

The form validator throws a "Field img must not be empty" error. var_dump() shows that the $_FILES array is empty.
Views

{{ content() }}
{{ form('', 'method':'post') }}
{{ form.render('name') }}
{{ form.render('description') }}
<span class="base f-12">Навык оружия</span>
{{ form.render('skill') }}
<label class="base f-12">Уровень оружия</label>
{{ form.render('lvl') }}
<label class="base f-12">Тип оружия</label>
{{ form.render('type') }}
{{ form.render('attack') }}
{{ form.render('price') }}
{{ form.render('strength') }}
{{ form.render('criticalChance') }}
{{ form.render('criticalDamage') }}
{{ form.render('img') }}
{{ form.render('submit') }}
{{ end_form() }}
<small class="base">* - обязательные поля</small>

Form
$img = new File('img');
        $img->addValidator (new FileValidator(
           [
                'maxSize' => '2M',
                'messageSize' => ':максимальный размер изображения (:max)',
                'allowedTypes' => ['image/jpeg', 'image/png'],
                'messageType' => 'Тип изображения должен быть :types',
                'maxResolution' => '100x100',
                'messageMaxResolution' => 'Максимальное разрешение изображения  :field  :max'
            ]

        ));
        $this->add($img);

controller
public function addWeaponAction() {
        $this->elements->setTitle('Добавить оружие');
        $form = new TradeAddWeapon();
            if (!$form->isValid($this->request->getPost())) {
                foreach ($form->getMessages() as $message) {
                    $this->flash->error($message);
                }
            }else {
                    if ($this->request->hasFiles() == true) {
                        $baseLocation = __DIR__ . 'public/img/items/weapon/';
                        foreach ($this->request->getUploadedFiles('img') as $file) {
                            $file->moveTo($baseLocation . $file->getName());
                            $item = new ItemsWeapon();
                            $item->name = $this->request->getPost('name', 'string');
                            $item->description = $this->request->getPost('description', 'string');
                            $item->lvl = $this->request->getPost('lvl', 'int');
                            $item->attack = $this->request->getPost('attack', 'int');
                            $item->type = $this->request->getPost('type', 'int');
                            $item->strength = $this->request->getPost('strength', 'int');
                            $item->price = $this->request->getPost('price', 'int');
                            $item->skill = $this->request->getPost('skill', 'string');
                            $item->critical_chance = $this->request->getPost('criticalChance', 'int');
                            $item->critical_damage = $this->request->getPost('criticalDamage', 'int');
                            $item->str = $this->request->getPost('str', 'int');
                            $item->dex = $this->request->getPost('dex', 'int');
                            $item->sta = $this->request->getPost('sta', 'int');
                            $item->int = $this->request->getPost('int', 'int');
                            $item->path_img = $baseLocation . $file->getName();
                            if ($item->save() == false) {
                                foreach ($item->getMessages() as $message) {
                                    $this->flash->error((string)$message);
                                }
                            } else {
                                $this->flash->success('Предмет успешно добавлен');
                            }
                        }

                    }

            }
        $this->view->form = $form;
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Pavlenko, 2016-08-01
@Gildars

Not familiar with Phalcon, but still...
Did you remember to add multipart/form-data to the form tag? https://en.wikipedia.org/wiki/Multipart/form-data
You need to add enctype="multipart/form-data" attribute to the form tag. It's just a common mistake. He himself allowed it several times, and was ready to tear his hair on his head ...
https://webref.ru/html/form/enctype here is more information

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question