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 AppsWebControllers
;

class 
SomeController extends PhalconMvcController
{
    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.
                        
}
                    }
                }
            }
        }
    }
}

Visit me on Google+

Leave a comment