Book Image

Learning Phalcon PHP

Book Image

Learning Phalcon PHP

Overview of this book

Table of Contents (17 chapters)
Learning Phalcon PHP
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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:

<?php
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.');
      }

      $this->response->redirect('media/add'...