We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

File not being uploaded

I have a hard trouble uploading a file to server, more exactly function hasFiles() return false each time my code is:

    protected function uploadForm() {
        $form = new Form();
        $form->add(new File("file", array()));
        $form->add(new Text("name", array()));
        return $form;
    }

    public function uploadAction() {
        $this->view->form = $this->uploadForm();
    }

    public function savefileAction() {

        $request = $this->request;

        if ($request->isPost()) {
            $this->flashSession->success('is post');

            $name = $request->getPost('name');

            $this->flashSession->success($name);

            if ($request->hasFiles() == true) {

                $this->flashSession->success('has files');

                foreach ($request->getUploadedFiles() as $file) {

                    $this->flashSession->success($file->getName(), " ", $file->getSize());

                    $file->moveTo('/public/img/' . $file->getName());
                }
            }
        }
    }

template:

    <?php echo Phalcon\Tag::form(array('admin/savefile',)); ?>

                <?php echo $form->render("file"); ?>
                <?php echo $form->render("name"); ?>

                <?php echo Phalcon\Tag::submitButton(array("submit")) ?>
    </form>

after submit the output s "is post" and "<name value>" so hasFiles() is always zero. please advice



1.6k
Accepted
answer

I haven't run the code but I suspect that the <form> tag doesn't have the enctype attribute:

<form enctype="multipart/form-data">

Maybe try this as the template:

<?php echo Phalcon\Tag::form(array('admin/savefile', 'enctype' => 'multipart/form-data')); ?>

        <?php echo $form->render("file"); ?>
        <?php echo $form->render("name"); ?>

        <?php echo Phalcon\Tag::submitButton(array("submit")) ?>
        </form>

The enctype="multipart/form-data" attribute is required for file uploads.



3.8k

thx. enctype was the issue



714

Thx! I had the same problem but I'm using Volt template files, the solution is the same

form("products_sale/create", "method" : "post", "class" : "form-horizontal" , "enctype" : "multipart/form-data") }}