Book Image

Learning Phalcon PHP

By : Calin Rada, Ioan C Rada
Book Image

Learning Phalcon PHP

By: Calin Rada, Ioan C Rada

Overview of this book

Table of Contents (17 chapters)
Learning Phalcon PHP
About the Author
About the Reviewers

Uploading files with Phalcon

Uploading files with Phalcon is a piece of cake. We just need to check whether the request object has files and move them to our upload directory. Let's create the following controller in the Backoffice module:

namespace App\Backoffice\Controllers;

use App\Core\Forms\MediaForm;

class MediaController extends BaseController {
  public function addAction() {
    $this->view->form = new MediaForm();

  public function uploadAction() {
    if (true == $this->request->hasFiles() && $this->request->isPost()) {
      $upload_dir = __DIR__ . '/../../../public/uploads/';

      if (!is_dir($upload_dir)) {
        mkdir($upload_dir, 0755);
      foreach ($this->request->getUploadedFiles() as $file) {
        $file->moveTo($upload_dir . $file->getName());
        $this->flashSession->success($file->getName().' has been successfully uploaded.');