Processing / Receiving file uploads with Phalcon

May 18th 2014

Here's a quick snippet of code to manage a file upload in Phalcon (a friend's asked for it and I'd rather stick it on here than a gist).

PHP Code:
<?php
namespace Apps\Web\Controllers;

class 
SomeController extends \Phalcon\Mvc\Controller
{
    public function 
myUploadAction()
    {
        
$path '/some/path/to/storage/';

        if (
$this->request->isPost()) {
            if (
$form->isValid($this->request->getPost())) {
                if (
$this->request->hasFiles(true)) {
                    foreach (
$this->request->getUploadedFiles(true) as $file) {
                        
/* @var $file \Phalcon\Http\Request\FileInterface */
                        
$fileName $file->getName();
                        
$temporaryName $file->getTempName();
                        if (
$file->moveTo($path $fileName)) {
                            
// OK! File uploaded and stored.
                            
$fileIsLocatedAt $path $fileName;
                        } else {
                            
// Oops, there's been a problem uploading.
                        
}
                    }
                }
            }
        }
    }
}