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

Multiple file upload

I want to upload to file in the process of a registration. There two file uploads named 'Photo' and 'Signature'. I want to upload the file with custom name in different folders. I search a lot but i can't file code.



16.2k

This is a pretty general question, but basically you will want to create two file fields using Phalcon\Tag, then check for files using Phalcon\Http\Request\File. Also make sure your 'enctype' in your form tag is "multipart/form-data"

In your form:

echo $this->tag->fileField('my_file');
echo $this->tag->fileField('my_file2');

Then in your controller where you $_POST your form to, something like this:

if ($this->request->hasFiles() == true) {
    foreach ($this->request->getUploadedFiles() as $upload) {
            $path = '/my/uploads/dir/' . $upload->getName();
            $upload->moveTo($path);
    }
}


24.2k

Thanks for your quick reply. But I want to move each file on seperate folder and update file name to table.

What you're asking is basically for code to do the whole upload process. That process involves lots of different aspects. Do a Google search for "PHP file upload" to get the basic process down. Then post questions about particular functionality.



24.2k

I want to know Phalcon PHP's way to do this. I can google for Native PHP's way to upload multiple files.



125.8k
Accepted
answer

Phalcon doesn't have any special functionality for moving files around on the server.

Updating a file name in a table is simply done with PHQL or ORM - both of which are mentioned in the docs.



24.2k

Tanks for help from all experts. Finally, I got solution

move_uploaded_file($_FILES['signature']['tmp_name'], 'files/signature/');

edited Dec '17
if ($this->request->hasFiles() == true) {
    foreach ($this->request->getUploadedFiles() as $upload) {
            if ($file->getKey() =='my_file')
                $path = '/my/uploads/dir1/' . $upload->getName();
            if ($file->getKey() =='my_file2')
                $path = '/my/uploads/dir2/' . $upload->getName();
            $upload->moveTo($path);
    }
}

This thread is 3 years old and marked as already solved. Further responses aren't needed.