We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Issue with uploading a single file

Hi everyone !

I'm really sorry about boring you with that kind of problem, but i've tried for hours to find my problem.

Few months ago i could upload a file without problem, but today i can't. I'm pretty sure it's a very little mistake in my code, but i can't find it.

In fact i know where is the error, but i don't understand why:

$this->request->hasFiles() /* returning always false !!!!  */

This is my code, i'll be really thankful, if you can help me on this !

/* ##### CONTROLLER ##### */
$albums = Albums::find();
$form = new ImagesForm($albums);
if($this->request->isPost())
{
    if($form->isValid($this->request->getPost()) != false)
    {
        var_dump($_FILES);
        if($this->request->hasFiles())
        {
            foreach($this->request->getUploadedFiles() as $file)
            {
                $image = new Images();
                $image->assign(array(
                    'title' => $this->request->getPost('title'),
                    'src' => 'img/photos/' . $file->getName(),
                    /* other things */
                ));
                if($image->save())
                {
                    $response = new Response();
                    return $response->redirect(array(
                        'for' => 'admin-index',
                        'controller' => 'control'
                    ));
                }
            }
        }
    }
}
/* ##### IMAGESFORM ##### */
$title = new Text('title', array(
    'class' => 'form-control'
));
$title->addValidators(array(
    new StringLength(array(
        'min' => 2,
        'max' => 255,
        'messageMaximum' => "Title is too long !",
        'messageMinimum' => "Title is too short !"
    ))
));
$this->add($title);

$image = new File('image',array(
    'class' => 'form-control'
));
$this->add($image);
$csrf = new Hidden('csrf');
$csrf->addValidator(new Identical(array(
    'value' => $this->security->getSessionToken(),
    'message' => "CSRF authentification failed !"
)));
$this->add($csrf);

$submit = new Submit('valider', array(
    'value' => 'Submit',
    'class' => 'btn btn-outline-inverse btn-lg'
));
$this->add($submit);


7.0k
Accepted
answer

My bad, i just find !

I forget this code in my view declaring the form:

enctype="multipart/form-data"

with the following code it works perfectly:

<form method="post" autocomplete="off" enctype="multipart/form-data">

Really sorry about that ...