IndexController.php 1.93 KB
<?php

namespace controllers;

class IndexController extends \Phalcon\Mvc\Controller
{
    ///////////////////////////////////////////////////////////////////////////


    public function indexAction()
    {
       // die(print_r($_SESSION['user-id']));
        if($_SESSION['user-id']){
            $user_id = $_SESSION['user-id'];
            $user = \users::findFirst(array("id = $user_id"));
            $role = \usersServices::find();

            $this->view->setVars([
                'role' => $role,
                'user' => $user
            ]);
        }


    }


    public function downloadImagesAction()
    {

        if ($this->request->hasFiles() == true) {

            $data['directory'] = $this->request->getPost('directory' );

            foreach ($this->request->getUploadedFiles() as $file){

                $allowed_filetypes = array('.jpg','.JPG', '.png', '.PNG', '.gif', '.GIF');

                $ext = substr($file->getName() ,strpos($file->getName() ,'.'),strlen($file->getName() )-1);

                if(!$data['directory']) {
                    $data['directory'] = md5(microtime());
                }


                if(in_array($ext,$allowed_filetypes))
                {
                    $image_path = $this->storage->getEmailTemplatePath( 'temp', $data['directory']);

                    if(!file_exists($image_path))
                    {
                        mkdir( $image_path, 0777, true );
                    }
                    $file->moveTo($image_path.$file->getName());
                    $data['message'] = 'Загрузка файла '.$file->getName().' выполнена успешно.';
                } else {
                    $data['message'] = 'Произошла ошибка. Не верный формат файла.';
                }
                $this->view->disableLevel(\Phalcon\Mvc\View::LEVEL_MAIN_LAYOUT);


                echo json_encode($data);


            }
        }
    }
}